|
You forgot to also fix the tests. Also, who says it's not public API?
|
# ? May 1, 2016 22:04 |
|
|
# ? May 9, 2024 18:01 |
|
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?
|
# ? May 2, 2016 05:36 |
|
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.
|
# ? May 2, 2016 05:47 |
|
Suspicious Dish posted:Nope. Python 2 counts the number of bytes in a bytestring. Python 3 counts the number of codepoints in a string. 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 |
# ? May 2, 2016 06:10 |
|
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? 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).
|
# ? May 2, 2016 12:02 |
|
So to plot a function f in matplotlib, you do something like this:Python code:
|
# ? May 2, 2016 12:54 |
|
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?
|
# ? May 2, 2016 18:53 |
|
Dominoes posted:So to plot a function f in matplotlib, you do something like this: 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.
|
# ? May 2, 2016 19:51 |
|
Could be difficult and imperfect, but certainly not impossible.
|
# ? May 2, 2016 20:02 |
|
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. 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.
|
# ? May 2, 2016 23:52 |
|
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)
|
# ? May 3, 2016 08:25 |
|
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?
|
# ? May 3, 2016 10:20 |
|
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.
|
# ? May 3, 2016 10:26 |
|
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)
|
# ? May 3, 2016 15:47 |
|
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 |
# ? May 3, 2016 15:56 |
|
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 |
# ? May 3, 2016 16:34 |
|
^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 |
# ? May 3, 2016 16:35 |
|
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.
|
# ? May 3, 2016 16:47 |
|
That's exactly what I'm asking.
|
# ? May 3, 2016 16:52 |
|
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.
|
# ? May 3, 2016 22:56 |
|
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 |
# ? May 4, 2016 09:12 |
|
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 |
# ? May 4, 2016 15:35 |
|
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. 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. 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:
|
# ? May 4, 2016 20:18 |
|
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. 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:
|
# ? May 4, 2016 21:07 |
|
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: I was using whatever the default backend is, because I had the following statements at the top of my *.py file Python code:
Python code:
|
# ? May 4, 2016 21:56 |
|
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.
|
# ? May 4, 2016 22:32 |
|
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.
|
# ? May 5, 2016 04:01 |
|
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:
|
# ? May 5, 2016 17:55 |
|
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. What's the error you are getting?
|
# ? May 5, 2016 18:19 |
|
HardDisk posted:What's the error you are getting? 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?
|
# ? May 5, 2016 18:26 |
|
code:
Edit: Ah, I missed that its specified with the execute statement.
|
# ? May 5, 2016 18:40 |
|
Loving Africa Chaps posted:Just doing tabs but for some reason the code tags make it look weird 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.
|
# ? May 5, 2016 18:41 |
|
Loving Africa Chaps posted:Just doing tabs but for some reason the code tags make it look weird I think your params need to be a tuple or dictionary: quote:cursor.execute(operation, params=None, multi=False)
|
# ? May 5, 2016 18:42 |
|
Loving Africa Chaps posted:Just doing tabs but for some reason the code tags make it look weird try removing the space from the equals and the %s. and maybe put twitterid in quotes: code:
https://mariadb.com/blog/how-connect-python-programs-mariadb
|
# ? May 5, 2016 18:42 |
|
Hegel posted:try removing the space from the equals and the %s. and maybe put twitterid in parens:
|
# ? May 5, 2016 18:43 |
|
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
|
# ? May 5, 2016 19:01 |
|
Loving Africa Chaps posted:looks like it was the missing comma after twitterid The comma changes the type from a string to a tuple, in case that wasn't clear.
|
# ? May 5, 2016 20:12 |
|
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
|
# ? May 5, 2016 20:15 |
|
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"
|
# ? May 5, 2016 21:37 |
|
|
# ? May 9, 2024 18:01 |
|
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. You may need to put the %s in quotes?
|
# ? May 6, 2016 17:14 |