Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
You forgot to also fix the tests. Also, who says it's not public API?

Adbot
ADBOT LOVES YOU

FoiledAgain
May 6, 2007

I have a question about unicode. I have a list of strings which looks like this: letters=['χˤ', 'ʊ̃ː', 'œi']. I got this list by reading from a file opened using encoding='utf-8'. I want to be able to do word=''.join(letters) to make string so that len(word)==3. Instead, I get a string where len(word)==7.How do I get Python to understand that 'ʊ̃ː', for example, is a single unit, and not three consecutive symbols?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Nope. Python 2 counts the number of bytes in a bytestring. Python 3 counts the number of codepoints in a string.

Unfortunately, there's no way to count the number of user-perceived characters ("extended grapheme cluster" in Unicode terminology) in Python. There are several third-party modules that apparently do it, though I've never used them.

The only language I know of that lets you easily count grapheme clusters is Swift.

FoiledAgain
May 6, 2007

Suspicious Dish posted:

Nope. Python 2 counts the number of bytes in a bytestring. Python 3 counts the number of codepoints in a string.

Unfortunately, there's no way to count the number of user-perceived characters ("extended grapheme cluster" in Unicode terminology) in Python. There are several third-party modules that apparently do it, though I've never used them.

The only language I know of that lets you easily count grapheme clusters is Swift.

Yeah, that's very unfortunate. But I'm still confused about something. I don't understand why Python reads 'ʊ̃ː' from my file in the first place, instead of reading 'ʊ~:'. Or does this have to do with the way that my IDE (PyCharm) displays stuff?

edit: I'm using Python 3.4

FoiledAgain fucked around with this message at 06:14 on May 2, 2016

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

FoiledAgain posted:

Yeah, that's very unfortunate. But I'm still confused about something. I don't understand why Python reads 'ʊ̃ː' from my file in the first place, instead of reading 'ʊ~:'. Or does this have to do with the way that my IDE (PyCharm) displays stuff?

edit: I'm using Python 3.4

It might be helpful to open the file as binary (open(filename, 'rb')) and tell us what Python reports as the contents. It would be easier to work out what's actually in the file and whether your expectations for what should be displayed are based on peculiarities of how the file contents are treated by this or that program you're using.

Also, it's easy to be misled when you take the contents of the file, as text displayed by some program, and paste them on the forums, because 1) the program you're using might be doing some mangling that's hard to identify and 2) the forums might be doing some mangling, because they do that sometimes with non-ASCII characters (particularly in code blocks).

Dominoes
Sep 20, 2007

So to plot a function f in matplotlib, you do something like this:

Python code:
    x = np.linspace(x_min, x_max, resolution)
    y = f(x)

    plt.plot(x, y)
    plt.show()
Is there a way to automatically set x_min and x_max based on making a reasonable-looking graph?

Jose Cuervo
Aug 25, 2004
Thanks to QuarkJets and accipter for pushing me in the right direction with PySide GUI programming. I broke things into classes, read the documents and figured out how to use the signals/slot mechanisms, and now have a decent looking GUI.

I now start my simulation in a separate thread than the GUI, so the GUI stays responsive while the simulation runs. However, I would like to be able to stop the simulation code running halfway through (essentially abort the simulation). Is there an equivalent of pressing Ctrl-C that I can program as code?

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Dominoes posted:

So to plot a function f in matplotlib, you do something like this:

Python code:
    x = np.linspace(x_min, x_max, resolution)
    y = f(x)

    plt.plot(x, y)
    plt.show()
Is there a way to automatically set x_min and x_max based on making a reasonable-looking graph?

How would it know what would result in a reasonable looking graph? I don't know matplotlib from a hole in the ground, but it sounds like it would be fairly difficult to implement something like that.

Dominoes
Sep 20, 2007

Could be difficult and imperfect, but certainly not impossible.

accipter
Sep 12, 2003

Jose Cuervo posted:

Thanks to QuarkJets and accipter for pushing me in the right direction with PySide GUI programming. I broke things into classes, read the documents and figured out how to use the signals/slot mechanisms, and now have a decent looking GUI.

I now start my simulation in a separate thread than the GUI, so the GUI stays responsive while the simulation runs. However, I would like to be able to stop the simulation code running halfway through (essentially abort the simulation). Is there an equivalent of pressing Ctrl-C that I can program as code?

Is the simulation happening in some sort of Python loop? Or an external program? If it is a Python loop, you should check the state of a variable (perhaps named ok_to_continue) and cancel if a slot has set this value to False. The when to check the variable would depend on the structure of the code and how long it takes to run. You would then connect a signal to this slot and cancel it via a button. Hopefully that short explanation makes sense.

If you call the simulation with a subprocess do a similar process, but you have to kill the subprocess.

QuarkJets
Sep 8, 2008

Yeah, assuming that your GUI is keeping track of the thread that's actually doing work you should be able to receive input from the user and then kill whatever that thread is doing.

And if you're interested in having this be done via some keyboard shortcut you can reimplement keyPressEvent. This function receives a QKeyEvent, which you can check for the pressed key value as well as any simultaneously-held modifiers (such as Ctrl, Alt, etc). Or using a button is good, too (and more intuitive but I love keyboard shortcuts)

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Dominoes posted:

Could be difficult and imperfect, but certainly not impossible.

No, not impossible, but a lot of effort to achieve results that the user will probably look at and decide to override with their own values anyway.

For a small set of types of functions out of a textbook (polynomials, trigonometric and hyperbolic functions, exponentials, other well-studied special functions, and sums/products/reciprocals/quotients of these various things) you could probably get good results, but it would be more work than is really justified for a minor convenience. For more exotic functions you'd be stuck, because how are you going to analyse the features of some special snowflake function and identify which bits are interesting to the user?

Dominoes
Sep 20, 2007

You don't need to analyze the features directly; just analyze the output at sampling points and produce a best-guess. Visualizing a function from a terminal takes several lines each time, and can take trial+error to set the right linspace. The first bit's easily solved with a wrapper function; the second, as you point out, is a challenge.

BigRedDot
Mar 6, 2008

Hammerite posted:

How would it know what would result in a reasonable looking graph? I don't know matplotlib from a hole in the ground, but it sounds like it would be fairly difficult to implement something like that.

It's fairly straightforward. You ask all the renderers to report back what their preferred bounds are (because they are the things that actually know how to account for e.g. the real radius of circles, or the extent of whatever they are drawing), and then union all them all together at the "plot" level. (That's how Bokeh implements auto-ranging, but I would expect something similar from MPL)

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

BigRedDot posted:

It's fairly straightforward. You ask all the renderers to report back what their preferred bounds are (because they are the things that actually know how to account for e.g. the real radius of circles, or the extent of whatever they are drawing), and then union all them all together at the "plot" level. (That's how Bokeh implements auto-ranging, but I would expect something similar from MPL)

I was assuming that when Dominoes referred to a "function", he meant that he had a function f: ℝ → ℝ (or a reasonable variation on this, say function undefined at at most countably many points of ℝ) and he wanted to plot the graph y = f(x). That is to say, that there is no defined extent, and he wants the software to guess one.

Hammerite fucked around with this message at 15:58 on May 3, 2016

BigRedDot
Mar 6, 2008

Hammerite posted:

I was assuming that when Dominoes referred to a "function", he meant that he had a function f: ℝ → ℝ (or a reasonable variation on this, say function undefined at at most countably many points of ℝ) and he wanted to plot the graph y = f(x). That is to say, that there is no defined extent, and he wants the software to guess one.

I don't understand this statement. All the x values are given (np.linspace returns an array of actual values) and so then also all the y values are determined as well as soon as y=f(x) is executed. At a bare minimum, an auto-range estimate could just be the envelope of this finite set of x,y pairs. But as I mentioned you can get better preferred bounds by asking the renderers (just taking the envelope would not account for the finite size of "dots" or other markers, but the renderers can factor this information in).

Edit: Perhaps the question is different. Looking back, I can't tell. If it's really about guessing the inputs to linspace and not merely about having a plot auto-range in reaction to whatever it is given, then sure, that's a weird notion.

BigRedDot fucked around with this message at 16:37 on May 3, 2016

Dominoes
Sep 20, 2007

^You got it; just looking for a way to auto-set linspace so the graph makes sense. Ie if you type in a function on wolfram alpha, it gives you a graph that shows what you want without configuring. Eg If you put in sin(x), it shows a nice 2-period wave; if you put in a quadratic function, you get a nice parabola instead of a squashed one. I'd bet there are a lot of approaches to solving this.

I'm getting ready to release the datetime module I talked about last page on PyPi. Any suggestions or requests before I submit? It provides a few functions to easily create and modify tz-aware datetimes, without the drawbacks and odd syntax of existing attempts.

Documentation on Github covers its use. I hacked in Arrow's string formatting and parsing.


Is there a difference between pytz.utc and datetime.timezone.utc? Seems to be the same, other than the latter's builtin and python 3.2 only.

Any thoughts on this:

quote:

Do y'all think these these func arguments should be set up with the most important arguments first (ie the datetime), per convention, or with the ones that are less likely to change first, which would make partial/currying easier?

Dominoes fucked around with this message at 17:16 on May 3, 2016

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

BigRedDot posted:

I don't understand this statement.

I'm saying, what I thought the question was is: You give the software a function, say y = f(x) = x³ + 3x + 9 or y = f(x) = sin(x) + cos(2x). The software has to guess what would be a good range of x-values to plot it over. You haven't actually calculated any values at this point.

Dominoes
Sep 20, 2007

That's exactly what I'm asking.

accipter
Sep 12, 2003

Dominoes posted:

That's exactly what I'm asking.

I don't really understand why you are looking for this, but you need to decide what is interesting to the plot. Perhaps taking derivatives and finding locations of maxima and minima would be a good place to start.

QuarkJets
Sep 8, 2008

Yeah this is the kind of thing where you could really wind up in the weeds, I have no idea how you would even begin creating a "reasonable" set of axes ranges for any arbitrary equation or set of data but I can imagine all sorts of solutions that would be wasteful in both computational and programming time. The problem is extremely ill-posed

I like the derivative idea, maybe use second derivatives, too, but maybe that's not necessary

QuarkJets fucked around with this message at 09:15 on May 4, 2016

Dominoes
Sep 20, 2007

You guys are frightfully dull!

Uploaded to PyPi. Fixes my issues with dts.

Dominoes fucked around with this message at 04:10 on May 5, 2016

Jose Cuervo
Aug 25, 2004

accipter posted:

Is the simulation happening in some sort of Python loop? Or an external program? If it is a Python loop, you should check the state of a variable (perhaps named ok_to_continue) and cancel if a slot has set this value to False. The when to check the variable would depend on the structure of the code and how long it takes to run. You would then connect a signal to this slot and cancel it via a button. Hopefully that short explanation makes sense.

If you call the simulation with a subprocess do a similar process, but you have to kill the subprocess.

QuarkJets posted:

Yeah, assuming that your GUI is keeping track of the thread that's actually doing work you should be able to receive input from the user and then kill whatever that thread is doing.

And if you're interested in having this be done via some keyboard shortcut you can reimplement keyPressEvent. This function receives a QKeyEvent, which you can check for the pressed key value as well as any simultaneously-held modifiers (such as Ctrl, Alt, etc). Or using a button is good, too (and more intuitive but I love keyboard shortcuts)

Yeah the simulation is happening in a for loop in a worker thread (QtCore.QThread) so I will work on implementing the solution you outlined.

Another unrelated (I believe) issue:
As part of the simulation I use matplotlib.pyplot to plot a number of figures that I save as .pngs. The first time I run the simulation (i.e., hit the run button) the simulation runs just fine and outputs the figures as expected. However, if the simulation finishes and I try and rerun the simulation, it runs up until it has to plot the figures again at which point the GUI hangs (not responding) and when I close the GUI I get the following error message in the console:

_tkinter.TclError: out of stack space (infinite loop?)
From my Googling of the error I believe it has to do with not closing plots properly, but I thought I was closing the plots properly with the call plt.close('all'). The plotting code is here:
Python code:
    fraction_of_day = 24.0 / time_delta
    num_buckets = int(scn.stop_time / time_delta)
    y_level = 0
    y_labels = []
    fuel_types = scn.fuel_info.keys()
    fuel_types.sort(reverse=True)
    for fuel_type in fuel_types:
        for name in reversed(scn.fue_info[fuel_type]):
            for time_int_idx, prob in PSO[(name, fuel_type)].iteritems():
                time_interval_start = time_int_idx / fraction_of_day
                time_interval_end = (time_int_idx + 1) / fraction_of_day
                plt.hlines([y_level], [time_interval_start], [time_interval_end],
                           linewidth=cat_amounts[(name, fuel_type)][time_int_idx],
                           colors=set_interval_color(scn, int(prob * scn.num_reps)), alpha=prob)

            y_labels.append(fuel_type + ', ' + name)
            y_level += 1
    plt.savefig(scn.output_path + '/SOProb_policy' + str(pol.policy_idx) + '.png',
                    transparent=True)
    plt.close('all')
Any guidance on how to go about troubleshooting this error? I can provide more information if needed

accipter
Sep 12, 2003

Jose Cuervo posted:

Yeah the simulation is happening in a for loop in a worker thread (QtCore.QThread) so I will work on implementing the solution you outlined.

Another unrelated (I believe) issue:
As part of the simulation I use matplotlib.pyplot to plot a number of figures that I save as .pngs. The first time I run the simulation (i.e., hit the run button) the simulation runs just fine and outputs the figures as expected. However, if the simulation finishes and I try and rerun the simulation, it runs up until it has to plot the figures again at which point the GUI hangs (not responding) and when I close the GUI I get the following error message in the console:

_tkinter.TclError: out of stack space (infinite loop?)
From my Googling of the error I believe it has to do with not closing plots properly, but I thought I was closing the plots properly with the call plt.close('all'). The plotting code is here:
Python code:
    fraction_of_day = 24.0 / time_delta
    num_buckets = int(scn.stop_time / time_delta)
    y_level = 0
    y_labels = []
    fuel_types = scn.fuel_info.keys()
    fuel_types.sort(reverse=True)
    for fuel_type in fuel_types:
        for name in reversed(scn.fue_info[fuel_type]):
            for time_int_idx, prob in PSO[(name, fuel_type)].iteritems():
                time_interval_start = time_int_idx / fraction_of_day
                time_interval_end = (time_int_idx + 1) / fraction_of_day
                plt.hlines([y_level], [time_interval_start], [time_interval_end],
                           linewidth=cat_amounts[(name, fuel_type)][time_int_idx],
                           colors=set_interval_color(scn, int(prob * scn.num_reps)), alpha=prob)

            y_labels.append(fuel_type + ', ' + name)
            y_level += 1
    plt.savefig(scn.output_path + '/SOProb_policy' + str(pol.policy_idx) + '.png',
                    transparent=True)
    plt.close('all')
Any guidance on how to go about troubleshooting this error? I can provide more information if needed

It seems like you are doing everything right with respect to closing figures. What matplotlib backend are you using? What if you change to Agg with:
Python code:
import matplotlib
matplotlib.use('Agg')

Jose Cuervo
Aug 25, 2004

accipter posted:

It seems like you are doing everything right with respect to closing figures. What matplotlib backend are you using? What if you change to Agg with:
Python code:
import matplotlib
matplotlib.use('Agg')

I was using whatever the default backend is, because I had the following statements at the top of my *.py file
Python code:
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
However adding
Python code:
import matplotlib
matplotlib.use('Agg')
above those imports appears to have solved the problem (the GUI no longer becomes unresponsive after I repeat the simulation 2, 3 or even 4 times). Do you mind explaining why it works / why you thought it would work?

accipter
Sep 12, 2003

Jose Cuervo posted:

Do you mind explaining why it works / why you thought it would work?

To be honest, I am not sure except that I had to do that to solve the memory issue in the past.

OnceIWasAnOstrich
Jul 22, 2006

My guess is that by default it is selecting a Qt based backend (which are interactive and like to exist in your Gui threads so they can do things) and after plotting it is interaction weirdly with your Qt setup or not releasing the thread properly. When you switch to agg matplotlib isn't touching Qt at all, just returning a raster image in a way that can't interfere with your Qt code. Why it doesn't happen the first time I have no idea and I'm not terribly interested in trying to find out.

Loving Africa Chaps
Dec 3, 2007


We had not left it yet, but when I would wake in the night, I would lie, listening, homesick for it already.

I'm trying to get started with python having used php in the past and i'm tearing my hair out at what should be something really simple.

I'd like to iterate through a list of twitter ID's, check if they're already in a database and if not add them. For the loving life of me i can't seem to get a simple query to work at all. Every time i google for an answer people are using a slightly different way of writing a query.


Python code:
	cursor2 = mariadb_connection.cursor()
	query = ("SELECT * FROM foamites WHERE twithandle= %s")

	#scrape using the twitter api
	#this bit works and i print to a .txt
	twitterid = user.id

    cursor2.execute(query, twitterid)
    numrows = cursor2.rowcount
    	
	#if doesnt return anything 
	print "not in database"

Space Kablooey
May 6, 2009


Loving Africa Chaps posted:

I'm trying to get started with python having used php in the past and i'm tearing my hair out at what should be something really simple.

I'd like to iterate through a list of twitter ID's, check if they're already in a database and if not add them. For the loving life of me i can't seem to get a simple query to work at all. Every time i google for an answer people are using a slightly different way of writing a query.


Python code:
	cursor2 = mariadb_connection.cursor()
	query = ("SELECT * FROM foamites WHERE twithandle= %s")

	#scrape using the twitter api
	#this bit works and i print to a .txt
	twitterid = user.id

    cursor2.execute(query, twitterid)
    numrows = cursor2.rowcount
    	
	#if doesnt return anything 
	print "not in database"


What's the error you are getting? Also, don't mix tabs and spaces. When I quote it looks like they are mixed, but not in the post? :confused:

Loving Africa Chaps
Dec 3, 2007


We had not left it yet, but when I would wake in the night, I would lie, listening, homesick for it already.

HardDisk posted:

What's the error you are getting? Also, don't mix tabs and spaces. When I quote it looks like they are mixed, but not in the post? :confused:

Just doing tabs but for some reason the code tags make it look weird

Here's the error:

Traceback (most recent call last):
File "test.py", line 12, in <module>
cursor2.execute(query, name)
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1


Edit i should clarify that i'm using mysql.connector which didn't want to install via pip until i specified the version number so maybe that's got something to do with it?

ynohtna
Feb 16, 2007

backwoods compatible
Illegal Hen
code:
query = ("SELECT * FROM foamites WHERE twithandle= %s")
This line seems to be missing a string value to be interpolated into the query.

Edit: Ah, I missed that its specified with the execute statement.

Proteus Jones
Feb 28, 2013



Loving Africa Chaps posted:

Just doing tabs but for some reason the code tags make it look weird

Here's the error:

Traceback (most recent call last):
File "test.py", line 12, in <module>
cursor2.execute(query, name)
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1


Edit i should clarify that i'm using mysql.connector which didn't want to install via pip until i specified the version number so maybe that's got something to do with it?

Don't SQL queries end with a ';' ? It's been years since I've interacted with a database using SQL. Most of my python scripts just use flat files for input and results.

Tigren
Oct 3, 2003

Loving Africa Chaps posted:

Just doing tabs but for some reason the code tags make it look weird

Here's the error:

Traceback (most recent call last):
File "test.py", line 12, in <module>
cursor2.execute(query, name)
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1


Edit i should clarify that i'm using mysql.connector which didn't want to install via pip until i specified the version number so maybe that's got something to do with it?

I think your params need to be a tuple or dictionary:

quote:

cursor.execute(operation, params=None, multi=False)
iterator = cursor.execute(operation, params=None, multi=True)

This method executes the given database operation (query or command). The parameters found in the tuple or dictionary params are bound to the variables in the operation.

Hegel
Dec 17, 2009

Loving Africa Chaps posted:

Just doing tabs but for some reason the code tags make it look weird

Here's the error:

Traceback (most recent call last):
File "test.py", line 12, in <module>
cursor2.execute(query, name)
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/#/foambot/venv/lib/python2.7/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1


Edit i should clarify that i'm using mysql.connector which didn't want to install via pip until i specified the version number so maybe that's got something to do with it?

try removing the space from the equals and the %s. and maybe put twitterid in quotes:


code:
cursor.execute("SELECT first_name,last_name FROM employees WHERE first_name=%s", (some_name,))
from the docs

https://mariadb.com/blog/how-connect-python-programs-mariadb

Hegel
Dec 17, 2009

Hegel posted:

try removing the space from the equals and the %s. and maybe put twitterid in parens:


code:
cursor.execute("SELECT first_name,last_name FROM employees WHERE first_name=%s", (some_name,))
from the docs

https://mariadb.com/blog/how-connect-python-programs-mariadb

Loving Africa Chaps
Dec 3, 2007


We had not left it yet, but when I would wake in the night, I would lie, listening, homesick for it already.

looks like it was the missing comma after twitterid

now i just need to work out why it thinks it's returned a negative number of rows!

Cheers for everyones help

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Loving Africa Chaps posted:

looks like it was the missing comma after twitterid

now i just need to work out why it thinks it's returned a negative number of rows!

Cheers for everyones help

The comma changes the type from a string to a tuple, in case that wasn't clear.

Loving Africa Chaps
Dec 3, 2007


We had not left it yet, but when I would wake in the night, I would lie, listening, homesick for it already.

Munkeymon posted:

The comma changes the type from a string to a tuple, in case that wasn't clear.

It wasn't and might explain my cool new errors. Got some more reading to do i guess

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Loving Africa Chaps posted:

I'd like to iterate through a list of twitter ID's, check if they're already in a database and if not add them.

Note that if this is a db that will have multiple concurrent connections, this is subject to race conditions depending on how your applications is structured.

conx1: "Hey db do you have this twitter id?"
db: "nope"
conx2: "Hey db, insert this twitter id!"
conx1: "You don't have it? Alright then, insert this twitter id!"
db: "gently caress off and die"

Adbot
ADBOT LOVES YOU

ironypolice
Oct 22, 2002

Loving Africa Chaps posted:

I'm trying to get started with python having used php in the past and i'm tearing my hair out at what should be something really simple.

I'd like to iterate through a list of twitter ID's, check if they're already in a database and if not add them. For the loving life of me i can't seem to get a simple query to work at all. Every time i google for an answer people are using a slightly different way of writing a query.


Python code:
	cursor2 = mariadb_connection.cursor()
	query = ("SELECT * FROM foamites WHERE twithandle= %s")

	#scrape using the twitter api
	#this bit works and i print to a .txt
	twitterid = user.id

    cursor2.execute(query, twitterid)
    numrows = cursor2.rowcount
    	
	#if doesnt return anything 
	print "not in database"


You may need to put the %s in quotes?

  • Locked thread