|
What's the best way to completely break out of a nested loop?
|
# ? Jan 14, 2011 15:40 |
|
|
# ? Jun 12, 2024 17:19 |
|
Bodhi Tea posted:What's the best way to completely break out of a nested loop? Raise an exception? vv
|
# ? Jan 14, 2011 16:24 |
|
Bodhi Tea posted:What's the best way to completely break out of a nested loop? Another option is to put your loops in a function and then return from the function. This doesn't work as well if you need to pass a lot of variables into the function though.
|
# ? Jan 14, 2011 17:50 |
|
I'm looking for some good Python questions to ask a potential candidate during an interview. I'm pretty new to being the interviewer, so I'm not real sure what to ask. The candidate's resume has mention of Python experience, but I need a series of questions that will hopefully tell me how experienced they are. These questions don't have to be brutally difficult or deal with things like Python internals, but I'd like to get some ideas of how much they know about Python.
|
# ? Jan 14, 2011 18:28 |
|
Bodhi Tea posted:What's the best way to completely break out of a nested loop? return for example code:
|
# ? Jan 14, 2011 19:42 |
|
spankweasel posted:These questions don't have to be brutally difficult or deal with things like Python internals, but I'd like to get some ideas of how much they know about Python. what do the inspect, dis, contextmanager libraries do? name one function in itertools and describe how to implement it (when in doubt, ask them to implement chain) can you write a flatten function i.e [[1],[[2,[[4]]] -> [1,2,4] - how does it deal with strings whats the key difference between wsgi in python 2 and 3 - why is this when might you use threading and when might you use multiprocessing why is it ",".join(iter) and not iter.join(",") what do __all__ __main__ __debug__ do name one data structure in the collections library and implement it (when in doubt ask them to implement an ordered dict why should you never post to python-dev asking to remove the gil new style vs old style classes - whats differences roughly what is a decorator? how does @property work? what's special about pythons sorting algorithm what's special about hashing a number why does 1 is 1 work and why must you never do it what is id() and why shouldn't you store the result what's wrong with def foo(a=None,x=[]): x.append(a) what's wrong with x = [[]]*5 can you tell me any of the output of 'import this' what might I find in __future__ (for bonus points, braces)
|
# ? Jan 14, 2011 20:53 |
|
there's a fair bit of low-level in there, plus the occasional in-joke/point trivia, but good list. here's what I interviewed on in a previous gig: What is the GIL? What approaches to be people take to work around this? What is the WSGI specification? How does mod_python differ from mod_wsgi? How do closures work in Python and what are their limitations? What additional closure-related functionality will be available with Python 3000? What XML parsing libraries are available in the python standard library? How would you add support UTF-8 to an existing Python application? Any gotchas? What is the difference between an integer and a long in Python? In Python 3? Why is r"ab\"c" a five character string while r"abc\" is a syntax error? What error could the following line generate, and how could it be made safe? formatted = "The value is %s"%some_function() (assume some_function is guaranteed to exist and not generate an exception on its own) What attribute must be defined on an object for it to be used in conjunction with the "with" statement? edit: I almost forgot, ONLY 3 DAYS TO REGISTER AT THE DISCOUNTED RATE FOR PYCON 2011 IN ATLANTA!. https://www.pycon.com I'm attending for the first time this year, I'm excited Lurchington fucked around with this message at 22:31 on Jan 14, 2011 |
# ? Jan 14, 2011 22:28 |
|
Spime Wrangler posted:I'd try just using pydot with pyparsing, which is what python-graph appears to be built on top of anyways. This looked perfect, until I see that the module is almost entirely focused on making pictures with GraphViz, and the representation doesn't even track nodes correctly. code:
It's okay...the Graph implementation I banged out in 5 minutes does what I need. code:
Stabby McDamage fucked around with this message at 23:22 on Jan 14, 2011 |
# ? Jan 14, 2011 22:35 |
|
tef posted:why is it ",".join(iter) and not iter.join(",") I thought I knew a fair bit of python, but I think I could learn from having some of the above answered, even if it's just a brief sketch. quote:can you tell me any of the output of 'import this' You don't really rate them on something like this, do you? It's just an easter egg...
|
# ? Jan 14, 2011 22:44 |
|
Stabby McDamage posted:You don't really rate them on something like this, do you? It's just an easter egg... How good a python programmer could they be if they hadn't been using it long enough, or didn't care about the community enough to have seen that before?
|
# ? Jan 14, 2011 22:52 |
|
zarg posted:Hi guys. You'll need to figure out how you're going to be interacting with this program. Is it a Windows GUI? If so, this could be a very tricky task, and one that Python may not be the best tool for. Is it a web-based interface? If so, Python can work...you'll be using something like httplib or twill or something. Command-line? That can work very well...you'll be using subprocess and, if it's an interactive shell, maybe pexpect. However, if it has an actual API for programs to interface with, that will be your best bet. This could be something like an XML/HTTP interface, in which case you'll be using dom and httplib to send/receive XML requests and responses. As to your current task, there are two ways to go about it. The best one is to try whatever method you're going to eventually use to interact with the app, and see if it succeeds. If not, it may not be running. You can also ask directly: is such-and-such process currently running? This depends on the OS. On Linux/UNIX, I'd use the command-line tool 'ps' to get the list, then search it. On Windows, you'll be doing some library calls...I don't have any experience there. As far as I know, there's no platform-independent module to do this. EDIT: Sailor_Spoon posted:How good a python programmer could they be if they hadn't been using it long enough, or didn't care about the community enough to have seen that before? I don't want to get into an interviewing argument, but I didn't know what that would output, and I know a decent amount of Python. I'd seen that poem before, but I didn't remember that "import this" produces it. Let me put it like this: I wouldn't mind if you added bonus points for knowing that bit of trivia, but I wouldn't ding anyone for not knowing it. A better question might be to show them the "Zen of Python" and the "Zen of Perl" and ask which is which. EDIT2: Ugh, I just read that Zen of Perl in detail, and memories of how bad Perl can get at large scale came flooding back. That poem is like developer Stockholm Syndrome. "Just because the code looks messy doesn't mean it is bad." echinopsis posted:matplotlib? I meant the "nodes and edges" kind of graph, not the wavy line kind. Stabby McDamage fucked around with this message at 23:09 on Jan 14, 2011 |
# ? Jan 14, 2011 22:55 |
|
Stabby McDamage posted:A better question might be to show them the "Zen of Python" and the "Zen of Perl" and ask which is which. haha, I do like that one
|
# ? Jan 14, 2011 23:13 |
|
zarg posted:tl;dr: Is there a tutorial for having python check to see if another application is running, or just interacting with other applications in general? I've never done any windows programming, but WMI looks like something that might do what I think it is that you want. Though, I'm not actually quite sure what that is. Are you hoping to talk to some service on multiple other machines? Or do you just want to know that radmin.exe is running on your computer? Do you know that RAdmin actually provides some kind of interface for you to talk to?
|
# ? Jan 14, 2011 23:22 |
|
Lurchington posted:plus the occasional in-joke/point trivia guilty as charged. I like to ask a variety of technical and odd questions to get a feel for things. knowing bits of trivia is a sign of paying attention to dev chatter/community Stabby McDamage posted:I thought I knew a fair bit of python, but I think I could learn from having some of the above answered, even if it's just a brief sketch. why is it ",".join(iter) and not iter.join(",") - because we only need one method in string, and not one method in every iterable. what's special about pythons sorting algorithm -timsort is magical what's special about hashing a number >>> [hash(x) for x in range(10)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] c.f dictnotes in the svn why does 1 is 1 work and why must you never do it - numbers stored boxed on heap, but -4 to 255 (approx) are cached what is id() and why shouldn't you store the result - id gives you back a magic number that represents a pointer if you compare the id of two live objects, it is a safe operation if you store the magic number there is no guarantee the object is still alive and it is possible to get false positives when bits are recycled. really, if you need to compare object identity, use 'is' quote:You don't really rate them on something like this, do you? It's just an easter egg... knowing the zen of python is a good sign of someone who understands the nature of python 'explicit is better than implicit' is a good python-ism i'd expect a competent python dev to know. I've dressed them up and there is a few jokes in there so i'm not 100% serious but I would probably ask these just to see what happens, but i've jokingly asked people how they go about solving tsp
|
# ? Jan 15, 2011 01:43 |
|
Stabby McDamage posted:You'll need to figure out how you're going to be interacting with this program. We mostly use a windows GUI but it does support some command line switches. I've already written a simple batch file that initiates connections to all the workstations I need to connect to, (read: gets me to the "please enter userid/password" window prior to getting the remote running) so all I really need python to do is something simple like below. Is a radmin connection window open? If yes, then put userid xxx and password xxx. If no, then quit. Loop until no. Ideally I'd like to put some sort of escape command just in case something goes wrong, but I can deal with that once I get something up and running in the first place. Any thoughts on how I would tell python what to look for? Is it even possible? I've already looked around on google and the radmin forums and it does not seem to be possible to include a user ID or password in the command line stuff in my batch file. I'm only keen to use python because it's what we are learning in my course, so I'd like to both practice it and be able to ask my prof questions.
|
# ? Jan 15, 2011 04:14 |
|
Stabby McDamage posted:This looked perfect, until I see that the module is almost entirely focused on making pictures with GraphViz, and the representation doesn't even track nodes correctly. Oh, i guess i was under the impression making pictures was the idea, probably because I've always used it to visualize existing data structures, and have never run across a package actually designed to represent and operate on the graphs themselves. I would usually just check for the existence of each node, add if they weren't yet there, and then create the edge. I'm usually working with relatively small graphs (10-50 nodes) so there weren't really any performance issues. FWIW, you can also use pydot.graph_from_edges(list_of_tuples) and it will make the nodes for you. However I spent some time poking through the networkx docs last night and today and I'm never. going. back. I swear at least once every month I stumble across some library that offloads a huge chunk of my workload and I gushily fall in love with python all over again. Now if only there were a simulink equivalent...
|
# ? Jan 15, 2011 08:28 |
|
zarg posted:We mostly use a windows GUI but it does support some command line switches. I've already written a simple batch file that initiates connections to all the workstations I need to connect to, (read: gets me to the "please enter userid/password" window prior to getting the remote running) so all I really need python to do is something simple like below. In that case, Python may not be your best bet. If there is really and truly no way to use this app other than the GUI, and you simply have to automate this program, then you'd be looking at some kind of GUI-driving macro tool, maybe AutoIt. Looking at what radmin does, I'm not sure what you need to automate with it. It looks like just another commercial VNC/RDP knockoff, right? If the goal is to automatically perform tasks on a number of remote computers, people generally use Active Directory policies or some other programmatic means of control. Getting a remote GUI and simulating mouse/keyboard events is a pretty painful alternative. EDIT: Can you give me a full, concrete task you'd like to automate? Spime Wrangler posted:Oh, i guess i was under the impression making pictures was the idea, probably because I've always used it to visualize existing data structures, and have never run across a package actually designed to represent and operate on the graphs themselves. I'm working with compiler call graphs, and visualizing is an intermediate step, but I much prefer dumping it out in Trivial Graph Format to yEd, where I can use multiple layout algorithms and analysis tools interactively. That's just so I can understand it...the main purpose of the graph is program-driven transformations. (This is a security research project at the compiler level.) Stabby McDamage fucked around with this message at 19:14 on Jan 15, 2011 |
# ? Jan 15, 2011 19:08 |
|
Stabby McDamage posted:In that case, Python may not be your best bet. If there is really and truly no way to use this app other than the GUI, and you simply have to automate this program, then you'd be looking at some kind of GUI-driving macro tool, maybe AutoIt. There is no API for RAdmin, but it does support command line switches like /connect, which launches the window that prompts users for their info. My goal right now is simply to launch that user info window, then fill in the user information automatically to complete the connection. I have to launch the same 20 connections multiple times daily at work, so I just want to be able to automate that. I'm checking to see if a certain process has been started on those machines, but writing code to automate that step is way, way beyond me. For now I just want to open the windows automatically so save me starting the same 20 connections over and over.
|
# ? Jan 15, 2011 21:57 |
|
Stabby McDamage posted:In that case, Python may not be your best bet. If there is really and truly no way to use this app other than the GUI, and you simply have to automate this program, then you'd be looking at some kind of GUI-driving macro tool, maybe AutoIt. He could use AutoIt's COM bindings in python. I gave a little example of how to do that on this StackOverflow answer.
|
# ? Jan 15, 2011 22:01 |
|
Python importing is starting to make me want to commit suicide. I have the following stacktrace when I try to import ModuleA: code:
Yakattak fucked around with this message at 19:24 on Jan 16, 2011 |
# ? Jan 16, 2011 19:06 |
|
Yakattak posted:Python importing is starting to make me want to commit suicide. probably, stuff like that happens to me. I have some global stuff in A, and I define the global logger in B using some of that stuff, and I want access to the logger B. It's a mess, it makes sense, but eh I wish it was slightly different
|
# ? Jan 16, 2011 19:20 |
|
Lurchington posted:probably, stuff like that happens to me. I have some global stuff in A, and I define the global logger in B using some of that stuff, and I want access to the logger B. It's a mess, it makes sense, but eh I wish it was slightly different What did you end up doing to remedy it?
|
# ? Jan 16, 2011 19:24 |
|
not writing circular dependencies in import statements this is a mixture of putting things together and apart in files break the dependencies by eliminating them or clearer boundaries with newer modules. you have bits in A and B that depend on each other, so you put them in C and A,B import C or you put all the co-dependent bits in A and so only B imports A can't give exacts without source though.
|
# ? Jan 16, 2011 19:46 |
|
Incidentally, that's another great reason not to use from X import Y. You can have two modules import each other, and use each other's functions in their own functions, as long as you defer the lookup until after initialization is done. Example: code:
Sneftel fucked around with this message at 20:10 on Jan 16, 2011 |
# ? Jan 16, 2011 20:03 |
|
Sneftel posted:Incidentally, that's another great reason not to use from X import Y. You can have two modules import each other, and use each other's functions in their own functions, as long as you defer the lookup until after initialization is done. That actually still fails: code:
|
# ? Jan 16, 2011 21:13 |
|
tef posted:not writing circular dependencies in import statements The way I have my code setup is that ModuleA is a logger, Module2 is a class that ModuleA uses to email a specified user with the error message made by ModuleA. Module3 gives Module2 the email of the specified user. Since Module2 and 3 both require logging, ModuleA is required to be imported. I imagine if I had diagrammed this up it'd be a clusterfuck of circular dependencies.
|
# ? Jan 16, 2011 22:25 |
|
Yakattak posted:The way I have my code setup is that ModuleA is a logger, Module2 is a class that ModuleA uses to email a specified user with the error message made by ModuleA. Module3 gives Module2 the email of the specified user. Since Module2 and 3 both require logging, ModuleA is required to be imported. I imagine if I had diagrammed this up it'd be a clusterfuck of circular dependencies. If you can, move one of the imports into a procedure -- that should break the loop.
|
# ? Jan 16, 2011 23:44 |
|
Janin posted:If you can, move one of the imports into a procedure -- that should break the loop. A procedure being..? I'm a scrub at Python.
|
# ? Jan 16, 2011 23:56 |
|
Yakattak posted:A procedure being..? I'm a scrub at Python. instead of code:
code:
|
# ? Jan 17, 2011 00:31 |
|
Janin posted:instead of I see, that makes sense. Thanks!
|
# ? Jan 17, 2011 00:37 |
|
Yakattak posted:The way I have my code setup is that ModuleA is a logger, Module2 is a class that ModuleA uses to email a specified user with the error message made by ModuleA. Module3 gives Module2 the email of the specified user. Since Module2 and 3 both require logging, ModuleA is required to be imported. I imagine if I had diagrammed this up it'd be a clusterfuck of circular dependencies. it is always logging code especially if you have to try logging an error in the bit that generates emails, as if that bit errors out what do you do? i would avoid putting imports into function calls, it buries errors, but gently caress it, it would mostly work here, that is until you try and emit a message while a module is loading. in logging code:
code:
|
# ? Jan 17, 2011 00:49 |
|
b0lt posted:That actually still fails:
|
# ? Jan 17, 2011 00:53 |
|
I have JSON strings that look like: '{iv:"AeoaE1eWTylwz7sTP8NSUg",salt:"lan13206tsA",ct:"Ued7w0WQZm8cRsufAkWj"}'. How do I get the Python JSON module to accept these JSON objects that don't have quotes around the keys?
|
# ? Jan 17, 2011 13:26 |
|
Evil Robot posted:I have JSON strings that look like: '{iv:"AeoaE1eWTylwz7sTP8NSUg",salt:"lan13206tsA",ct:"Ued7w0WQZm8cRsufAkWj"}'. How do I get the Python JSON module to accept these JSON objects that don't have quotes around the keys? That's not valid JSON, and most parsers won't take it. Valid JSON requires the key to be a string literal or a number. A quick bit of googling says that demjson.py will accept your form of invalid JSON. Haven't tried it myself, but it's worth a shot, I suppose.
|
# ? Jan 17, 2011 16:25 |
|
Evil Robot posted:I have JSON strings that look like: '{iv:"AeoaE1eWTylwz7sTP8NSUg",salt:"lan13206tsA",ct:"Ued7w0WQZm8cRsufAkWj"}'. How do I get the Python JSON module to accept these JSON objects that don't have quotes around the keys?
|
# ? Jan 17, 2011 19:46 |
|
zarg posted:My goal right now is simply to launch that user info window, then fill in the user information automatically to complete the connection. I have to launch the same 20 connections multiple times daily at work, so I just want to be able to automate that. I'm checking to see if a certain process has been started on those machines, but writing code to automate that step is way, way beyond me. For now I just want to open the windows automatically so save me starting the same 20 connections over and over. I'm getting close a solution here. I've got some issues to resolve still, but I'm getting promising results. I've veered off of the python path because I think you guys are right and it's not the best tool, but since you have all been very helpful I figured I'd keep posting here to share my progress so far Step 1: .bat file launches 20 connections, bringing up a window requesting user information. Step 2: Execute a macro in macroexpress (the only macro app currently OK'ed for use here) which is simply "userid<TAB>password<ENTER>" to autocomplete the login. This macro is password protected for use and edit with the password it is entering, so effectivly it asks the user for the information once then auto-completes subsequent entries, if I can get the macro to repeat for all open connection windows correctly. Problems: Step 1: My .bat only launches one connection window at a time. It opens the window, lets me do what I want, then when I close it the next window opens. Instead I want every connection command to launch its own window. I'm not sure how to overcome this but I'm working on it. Suggestions would be fantastic. Linking a bunch of 1 line bats together is less than ideal, as I'm actually going to end up making 10 .bat files with 20 connections each, so that would total 200 linked .bat files Step 2: Macro applies to the current window only. There are some nice repeat functions, (until is one of them) in the macro app I'm using, (Macro Express) but I'm not sure how to force it to repeat for a specific type of window until that type is not found. It can identify windows, but it actually seems to think that each different IP is a unique window "type," so I'm not sure how to make it look for ANY radmin connection window just yet. zarg fucked around with this message at 22:24 on Jan 17, 2011 |
# ? Jan 17, 2011 22:19 |
|
Janin posted:Have you considered using real JSON instead of whatever that is? Yeah, some other library is giving me back this crap. Did some regexps to put in the appropriate quotes :/.
|
# ? Jan 17, 2011 22:50 |
|
Evil Robot posted:Yeah, some other library is giving me back this crap. Did some regexps to put in the appropriate quotes :/. I had a similar problem, since the output ended up being correct python, i just used ast.literal_eval.
|
# ? Jan 18, 2011 01:16 |
|
I'm having an issue with os.walk() and unicode paths. There's a directory that looks like this: code:
code:
code:
Running on Debian lenny with locale is en/utf8
|
# ? Jan 18, 2011 12:51 |
|
|
# ? Jun 12, 2024 17:19 |
|
file paths aren't in unicode, they're bytestrings on most platforms :worms: there was some hoo-haa about it around python 3 and the file-io stuff. I think you'll need to encode the unicode before using it
|
# ? Jan 18, 2011 13:16 |