|
Highblood posted:Stupid question incoming: Big newbie to programming here. What's the point of dictionaries? I'm used to the more C-like struct and they seem similar but I don't get the differences. Why assign values to strings? Why not just a list of variables (man reaching for that " key is really hard okay)? From what I understand you can add and remove entries to the dictionary whenever you want but the applications for that fly completely over my head (I'm dumb sorry) Python has dict, C++ has std::unordered_map. Associative containers are extremely useful for a variety of use cases. For instance, in Python keyword arguments are passed basically as a dictionary; this allows you to check for and use optional arguments without mandating that they exist and without having to define several versions of the same function/class. And deep down, most (all?) Python objects are actually just dictionaries anyway... so there's a use case
|
# ? Jan 11, 2016 19:50 |
|
|
# ? May 9, 2024 13:06 |
|
Hammerite posted:In the context of general Python programming, I would interpret "array" as synonymous with "list", since lists are the closest match for the notion of array in the Python standard library. In the context of using Python with Numpy, there might be an argument that this no longer holds; but as discussed, Numpy was at best implicitly introduced to the discussion context. In the context of scientific python it's reasonable to assume the default unit of stuff is a numpy array. There is a whole world of python users for whom python without numpy is simply not a thing.
|
# ? Jan 11, 2016 23:02 |
|
Nippashish posted:In the context of scientific python it's reasonable to assume the default unit of stuff is a numpy array. There is a whole world of python users for whom python without numpy is simply not a thing. On the other hand, it seems "import pandas as pd" is a part of like 75% of the answers in this thread. The main stuff that's not commonly helped by Pandas seems to be offering web services.
|
# ? Jan 12, 2016 00:12 |
|
Highblood posted:Stupid question incoming: Big newbie to programming here. What's the point of dictionaries? I'm used to the more C-like struct and they seem similar but I don't get the differences. Why assign values to strings? Why not just a list of variables (man reaching for that " key is really hard okay)? From what I understand you can add and remove entries to the dictionary whenever you want but the applications for that fly completely over my head (I'm dumb sorry) Apart from the excelent reasons already given, think about a system or an application with hundreds or thousands of components. If you use structs (or the equivalent in other languages) you are dealing with data which resides at a fixed offset from the beginning of the structure. Now, if you have to add something to a structure which is widely used in your system (and you WILL need to do it, sooner or later) you will have to be very careful to add everything in a way that does not break any of your components (usually adding new stuff in the tail of the struct is enough, but that is not always posible). If you can't guarantee the offsets of the current variables are not affected, you will have to recompile everything. If you use a dict and you need to add a new attribute, you just do it in runtime and just modify the components that need to use the new attribute. The other ones are not affected in any way.
|
# ? Jan 12, 2016 18:37 |
|
For purely educational purposes I've been trying different ways to extract information from a list of dictionaries. I know how to easily accomplish this with a list comprehension, but I'm wondering if it can also be done using map. The dict: code:
code:
code:
BennyGsGhost fucked around with this message at 19:29 on Jan 13, 2016 |
# ? Jan 13, 2016 19:26 |
|
You need to use filter as well as map. Also, for educational purposes, you may be able to replace the lambdas with something from the operator module.
|
# ? Jan 13, 2016 19:29 |
|
SurgicalOntologist posted:You need to use filter as well as map. Also, for educational purposes, you may be able to replace the lambdas with something from the operator module. Thanks for the suggestion, I got it working. Also figured out how to utilize operator.itemgetter to clean it up a bit based on the extra suggestion, thanks for the idea!
|
# ? Jan 13, 2016 22:29 |
|
Quick, dumb stupid question: what's the simplest way to make a "beep" sound in Python. Longer question, with context: I want to write a simple dumb little script that beeps at regular intervals until the script is terminated. That's all it should do. But the audio picture in Python looks completely fragmented. I'm on OSX, so that's the primary platform, but is there a simple way through this program or am I going to have to make a sound file and play it?
|
# ? Jan 18, 2016 17:15 |
|
outlier posted:Quick, dumb stupid question: what's the simplest way to make a "beep" sound in Python. Python code:
But then it only works in a terminal and some terminals are configured to do something different with the bell character.
|
# ? Jan 18, 2016 17:21 |
|
If you're on OSX you can also do os.system('say X') which can be a lot of fun.
|
# ? Jan 18, 2016 17:26 |
Is it common to have a method that can take multiple data types for the same parameter? I have a parameter that I want to accept either a string or a list. The string would just be for convenience instead of always having to pass in a list of 1. Something like: code:
|
|
# ? Jan 19, 2016 23:21 |
|
Super common, although ideally you just use duck typing. The "check the type" (or duck check the type or however you call it - basically, just check if it quacks like a duck) is also very common though.
|
# ? Jan 19, 2016 23:24 |
|
fletcher posted:Is it common to have a method that can take multiple data types for the same parameter? I have a parameter that I want to accept either a string or a list. The string would just be for convenience instead of always having to pass in a list of 1. In Python 2 you use isinstance(thing, basestring) In Python 3 there isn't any such thing as basestring any more and you just use isinstance(thing, str)
|
# ? Jan 20, 2016 11:15 |
|
Cingulate posted:Super common, although ideally you just use duck typing. You mean by picking a random method which strings have but lists don't have, and testing whether the parameter has that method?
|
# ? Jan 20, 2016 13:36 |
|
But man lists and strings is a hairy situation where the "type distinction" can be confusing, I'm not sure if I'm a big fan of "duck typing" in this case... Because of this I'd be careful with writing a function that takes both strings and lists of strings, maybe I'd separate into 2 different functions.
|
# ? Jan 20, 2016 14:30 |
Does anyone have experience using selenium to work with dropdown menus? Mainly looking for help in speeding this script up based on where I think the slowdown is. Part of my job has me cleaning up noisey data that comes in every day. It's through a vendor's system, so I can't perform operations on the data using SQL or anything else. I have to use this (really poorly put together) webpage to assign correct values to incorrect records. The script is based in Selenium and works pretty well, but it takes a long time towards the end of each loop of the script. The slowdown occurs when the script is navigating dropdown values and searching for the correct value. The script has to select values in three drop down menus, so I just made it into a function that takes the name of each dropdown menu, and the text it should be looking for. The dropdown menus get progressively longer, so by the time the script gets to the final dropdown menu it can take 5 to 10 seconds to identify the option to click. There are about 400~ dropdown items for it to iterate through by the end. code:
|
|
# ? Jan 20, 2016 15:36 |
|
Or just say "screw it" and require the caller to put that string in a single-item list. It's not that hard; I've been down that road before and I'm now convinced that the flexibility is not worth the trouble, even if the single-item call is by far the most common. Another idea, if there aren't a lot of other parameters, is to use *args. Python code:
|
# ? Jan 20, 2016 15:37 |
|
qntm posted:You mean by picking a random method which strings have but lists don't have, and testing whether the parameter has that method? It really depends I guess.
|
# ? Jan 20, 2016 16:47 |
|
I'll usually try to do the thing I want to do with the parameter and then catch exceptions to find out what went wrong.
|
# ? Jan 20, 2016 17:46 |
|
I am trying to use a Python module pacparser which is basically a wrapper for a C library. When the C library encounters an error, it spits out information and then raises a Python exception.Python code:
code:
Tigren fucked around with this message at 22:35 on Jan 20, 2016 |
# ? Jan 20, 2016 21:15 |
|
TryPython code:
|
# ? Jan 20, 2016 21:27 |
|
Pythonista on iOS is a neat piece of kit if you enjoy hobbyist Python development. Ver 2.0 exposes obj-C APIs so you can look up phone battery status, read in current GPS location, interact with Bluetooth devices (e.g., read in BPM from a BT heart rate monitor). On the newer iPhones, you can also tie scripts to 3D touch shortcuts which makes app-replacement py scripts plausible. I spent a couple hours writing a decent Japanese ebook reader with auto-generated translations shown side-by-side.
|
# ? Jan 20, 2016 21:47 |
|
Munkeymon posted:Try And where am I supposed to be seeing the output that I want to capture? Before resetting sys.stderr, sys.stderr is just <open file '/dev/null', mode 'w' at 0x7fad40d5ec90>
|
# ? Jan 20, 2016 22:35 |
|
Tigren posted:And where am I supposed to be seeing the output that I want to capture? Oh sorry I got it in my head that you just wanted to suppress it. Python code:
|
# ? Jan 20, 2016 22:45 |
|
Munkeymon posted:Oh sorry I got it in my head that you just wanted to suppress it. Python code:
|
# ? Jan 20, 2016 23:56 |
Hammerite posted:In Python 2 you use isinstance(thing, basestring) This was for python 2 & 3 using six.string_types =)
|
|
# ? Jan 21, 2016 00:00 |
|
Tigren posted:
You'd still have to do something about the exception: Python code:
|
# ? Jan 21, 2016 17:18 |
|
fletcher posted:This was for python 2 & 3 using six.string_types =) Oh, I'm not familiar with it. In that case I would do exactly what you did
|
# ? Jan 21, 2016 17:20 |
|
Munkeymon posted:You'd still have to do something about the exception: Thanks for being patient with me. stderr/stdout redirection seems like kind of a funky beast. I tried running that from within a REPL and from a script code:
Once I realized what you were doing, I looked into stderr redirection and saw mention of stderr caching. Do you think the JSERROR is cached and therefore not directly accessible from sys.stderr redirection?
|
# ? Jan 21, 2016 18:36 |
|
Tigren posted:Thanks for being patient with me. stderr/stdout redirection seems like kind of a funky beast. Caching shouldn't be an issue because we're supplying our own buffer before the output is generated. Could be I made an invalid assumption and it might be sending the output on stdout. Could be that there's an init in the module that grabs copies of the std* streams before we're replacing them. This should mitigate both: Python code:
|
# ? Jan 21, 2016 19:40 |
|
Munkeymon posted:Caching shouldn't be an issue because we're supplying our own buffer before the output is generated. Could be I made an invalid assumption and it might be sending the output on stdout. Could be that there's an init in the module that grabs copies of the std* streams before we're replacing them. This should mitigate both: code:
|
# ? Jan 21, 2016 22:28 |
|
I'm writing a python script that downloads the National Vulnerability Database from the nist.gov website and writes an xml file zipped into the directory. The other script I wrote that notifies the appropriate people in IRC with vulnerabilities that match the software and vendors we're using, just a hobby project while I learn python. That script works fine, however I can't seem to get this simple download using urllib2 to actually write the information to the new zip. It comes out as a blank XML file. What am I doing wrong?Python code:
|
# ? Jan 22, 2016 02:01 |
|
Frank Viola posted:I'm writing a python script that downloads the National Vulnerability Database from the nist.gov website and writes an xml file zipped into the directory. The other script I wrote that notifies the appropriate people in IRC with vulnerabilities that match the software and vendors we're using, just a hobby project while I learn python. That script works fine, however I can't seem to get this simple download using urllib2 to actually write the information to the new zip. It comes out as a blank XML file. What am I doing wrong? I'm not going to answer your question, but I just wanted to note that you might want to use the requests library instead of urllib2. I very rarely see anyone using urllib2 anymore...everyone seems to use requests.
|
# ? Jan 22, 2016 03:08 |
|
Thermopyle posted:I'm not going to answer your question, but I just wanted to note that you might want to use the requests library instead of urllib2. I very rarely see anyone using urllib2 anymore...everyone seems to use requests. 'wb'(binary) instead of 'w', rookie mistake; thanks for not telling me. Also I'll read up on requests. Thanks. Modulo16 fucked around with this message at 07:01 on Jan 22, 2016 |
# ? Jan 22, 2016 06:38 |
|
Frank Viola posted:I'm writing a python script that downloads the National Vulnerability Database from the nist.gov website and writes an xml file zipped into the directory. The other script I wrote that notifies the appropriate people in IRC with vulnerabilities that match the software and vendors we're using, just a hobby project while I learn python. That script works fine, however I can't seem to get this simple download using urllib2 to actually write the information to the new zip. It comes out as a blank XML file. What am I doing wrong? Also, rather than reading everything into memory at once then writing it all out at once, you might want to take a look at shutil.copyfileobj, context managers and contextlib.closing Python code:
|
# ? Jan 22, 2016 09:49 |
hcenvirons posted:question about selenium and selecting from dropdown I managed to solve the issue I had (holla @ stackoverflow questions from 2011) Instead of making a dropdown object and iterating through all the options it contains, I used Selenium's built in Select. code:
|
|
# ? Jan 22, 2016 16:10 |
|
SurgicalOntologist posted:If you're on OSX you can also do os.system('say X') which can be a lot of fun. This is amazing. Thank you. I can now annoy the hell out of my office mates. e: Holy poo poo git log 2>&1 | say -f - KernelSlanders fucked around with this message at 18:48 on Jan 24, 2016 |
# ? Jan 24, 2016 18:45 |
|
Does anyone know if NumPy/SciPy work with IronPython in C#? I am able to execute Python code and scripts just fine from within C#, but when I try to use one of my Python scripts that imports numpy, I immidately get an error "No module named numpy". My googling says that this wasn't possible a few years ago but all the questions about using SciPy/NumPy in Ironpython are now years old and it's unclear to me whether or not this is possible.
|
# ? Jan 24, 2016 21:52 |
|
As a general rule the numeric python ecosystem only works with CPython.
|
# ? Jan 24, 2016 21:56 |
|
|
# ? May 9, 2024 13:06 |
|
Is there a clean way to index a numpy array with interval, starting at the end instead of begining? code:
edit: Think I found it: x[::-3][::-1]
|
# ? Jan 25, 2016 00:22 |