|
This is a dumb question, but I'm having an impossible time trying to debug a multi-threaded program being run remotely. I am running pdb and think I found the call that causes my program to hang. Basically, when the get() function in Python's multiprocessing module is called, my program stops. When I inspect the get function, it lists: <built-in method recv of _multiprocessing.Connection object at 0xd3683e40> Is there any way in pdb to say, "get me the object at 0xd3683e40" so I can inspect it to see what it is, as I only have access to the function, not the object the function came from. Thanks!
|
# ? Nov 28, 2014 21:30 |
|
|
# ? Jun 2, 2024 22:59 |
|
Ultra newbie question here. I'm working my way through a Python Text Adventure platform from the ground up on my own, without any sort of book. I'm thinking I'd like to spend a bit to refactor before it gets unwieldy, as right now everything is in main.py. I'm having a hard time getting a handle on including things out, though. I want to move my item class definition and instances (nothing is database driven, I'm just hand creating them in the file), room class definition and instances, and mob class definition and instances to their own files, but there's a lot of cross talk. For instance, for my items, I've got them using lambdas to handle use so that a healing potion can do player.hp += 10 or automatically add an instance of an item to inventory or teleport a player to a specific room. Works fine when everything is in the same file, but as soon as I start splitting things into separate files, items can't see "player" or my maps, etc. Is this because I'm just not coding in a way that Python works well in, or am I missing something that allows things to play nice? I get having everything be completely self-contained, but if I can't have instances seeing each other that's a serious blow to my methodology.
|
# ? Nov 29, 2014 04:30 |
|
Are these instances globals? That's something you should try to avoid. There may be other solutions, but a straightforward one is to add arguments to all these functions. So instead of adding 10 hp to the global player instead, add 10 hp to the player instance that was input. This kind of design will aid in generality, now these potions will work on anything with an hp attribute, which may come in handy if you ever get to npc ai for example. Edit: you may also want to put the instance creation separate from the classes. That's what makes more sense to me at least; separate the platform from from the game you're making with it. SurgicalOntologist fucked around with this message at 04:49 on Nov 29, 2014 |
# ? Nov 29, 2014 04:46 |
|
Yeah, it just sounds like you haven't quite wrapped your head around some OOP concepts yet, which is fine -- they're not at all intuitive until one day they totally are. Here's a super quick, dirty mock-up that I hope might send you down the right path, at least. You could put each of those classes in their own file and just import them where you need them. The idea is that each kind of thing exposes an interface that other things can use to get only the information they need to do their job.
|
# ? Nov 29, 2014 05:21 |
|
I've been doing VERY BASIC OOP for awhile, just nothing like this and obviously very sloppy. At least Python is forcing me to clean up, which is great. It means I'm going to have to start over, though. I thought about going the route of having everything have methods for all the possible interactions (heal, damage, etc), but thought it would get unwieldy and tried to go with lambdas - seems I jut made more of a mess that way. Using globals is right on the nose. I'd create all my instances of rooms, items, mobs and the player and then keep a global variable of which room the player is in, but every single method needed to pull in that room id and then check getRoomByCurrentID(current_room)... drat. I had just about everything done - all the verb functions for moving, using (including use x on y), attacking, picking up, dropping, equipping, giving, quests... But it's all slipshod and I need to learn this proper. I think I'm just going to need to find a resource for something like this. I had all of it tied to a game state loop that just called the same functions over and over. I'm trying to think of how to structure the game more OOP. A master game class with methods for each phase maybe. Suppose it's time to research, but at least it's not all in vain. Thanks all!
|
# ? Nov 29, 2014 05:44 |
|
Cingulate posted:Ah, knew there'd be some map-like thing in there. Thanks. I actually tried passing a map to pandas in the first example like this Python code:
|
# ? Nov 29, 2014 05:50 |
|
Mortanis posted:I've been doing VERY BASIC OOP for awhile, just nothing like this and obviously very sloppy. At least Python is forcing me to clean up, which is great. It means I'm going to have to start over, though. I thought about going the route of having everything have methods for all the possible interactions (heal, damage, etc), but thought it would get unwieldy and tried to go with lambdas - seems I jut made more of a mess that way. You don't necessarily have to start over. You could start by making everything that's a global variable, instead be an attribute of an instance of a GameState class. It would probably make sense to have some of those functions be its methods.
|
# ? Nov 29, 2014 06:53 |
|
Mortanis posted:At least Python is forcing me to clean up, which is great. It means I'm going to have to start over, though. I thought about going the route of having everything have methods for all the possible interactions (heal, damage, etc), but thought it would get unwieldy and tried to go with lambdas - seems I jut made more of a mess that way. Python code:
That's a very simplified example and glides over some of the pitfalls of inheritance and such but that's the basic idea -- segregate functionality in the smallest units that make sense and combine them as needed. KICK BAMA KICK fucked around with this message at 07:29 on Nov 29, 2014 |
# ? Nov 29, 2014 07:24 |
|
No, that's helpful, thanks. I've been over all the OOP stuff before in other languages, but somehow completely forgot to apply it to Python and just was using objects as basic data shortage containers. This presents all sorts of new challenges, but that's that's learning the "right" way is better than locking myself into something sloppy. A rewrite would be good. Double Edit: Removing my question. I took a second look and figured it out. Indentation is important! Mortanis fucked around with this message at 02:16 on Nov 30, 2014 |
# ? Nov 29, 2014 20:42 |
|
I have a python project that seems to be more about sqlite than about python, so if this is the wrong thread for this question please forgive me. I've never used databases before and while I get how to make tables and insert poo poo into them, I don't get my instructions. I'm supposed to have table Orders USE table Inventory and table ClientInfo MAKE Orders. I looked into it and read something about joining columns but then it launched into keys and aliases and basically I'm kind of lostPython code:
Edit: Nevermind, I figured it out. Thanks anyways guys! FAT32 SHAMER fucked around with this message at 04:35 on Dec 2, 2014 |
# ? Dec 2, 2014 03:42 |
|
I'm trying to use subprocess.Popen() with a process that generates data forever at a high rate. I'd like it so that before each call to readline(), the pipe buffer is cleared such that I'm always reading the latest output, and anything that was received between calls to readline() falls into a blackhole. Alternatively, I'd like a tiny pipe buffer, and writes from the child process to simply disappear when the buffer is full, as opposed to blocking. But I'm guessing that will require modifying the linux kernel.
|
# ? Dec 3, 2014 03:43 |
|
zergstain posted:I'm trying to use subprocess.Popen() with a process that generates data forever at a high rate. I'd like it so that before each call to readline(), the pipe buffer is cleared such that I'm always reading the latest output, and anything that was received between calls to readline() falls into a blackhole. I can't think of any syscall that would allow you to do that. There's some bufferbloat stuff in-progress for the next version of Linux that would allow you to do this through the network stack, by having queue logic that drops packets from the end if the buffer is full, but that would be a massive hack. The issue is that this is the opposite way around to the normal operation, as usually it's the reader that blocks waiting for the writer to provide data, whereas you want the writer to block waiting for the reader to be reading data. quote:Alternatively, I'd like a tiny pipe buffer, and writes from the child process to simply disappear when the buffer is full, as opposed to blocking. But I'm guessing that will require modifying the linux kernel. If you can modify the process that's sending the data to set non-blocking mode on its output file descriptors, have it put a message length at the beginning of each message, and just silently drop writes where it either returned EAGAIN/EWOULDBLOCK, or write returned less than the message length, but you'd have to have the reader be reading the whole socket buffer in at once to be able to notice that it has to discard the message. Honestly this sounds closer to SOCK_SEQPACKET instead, where writes will fail if the message is unable to fit in the buffer. You'll still have at least 1 really old message in the buffer that you need to get through before new messages will be available, but if the message includes a timestamp you could drop old messages. http://man7.org/conf/lca2013/IPC_Overview-LCA-2013-printable.pdf may be relevant, as I can't think of any IPC primitive that would do what you want, but rather than putting stuff in the kernel, you may be able to implement it on top of shared memory as a rotating buffer. As an alternative to all this fiddling about with IPC primitives, you could try piping your process through svlogd, and when you want to read a message, you open the file path it's writing messages to and read that file. Things get complicated when you reach the end of the file, as you need to re-open the path and use os.fstat to see if the inode of the new file is different. If that's the case then you have to start reading from the new one while closing the old one, if not then svlogd is still writing to that file, and you have to wait for it to provide more data. You could lose messages if svlogd rotates yet another file between you finishing reading one file, and checking to see if there's a new file to read, but given you want to discard old messages anyway, this should be fine.
|
# ? Dec 3, 2014 13:15 |
|
zergstain posted:I'm trying to use subprocess.Popen() with a process that generates data forever at a high rate. I'd like it so that before each call to readline(), the pipe buffer is cleared such that I'm always reading the latest output, and anything that was received between calls to readline() falls into a blackhole. I probably read far too much into your request, so if you just want to discard the contents of the buffer then do a blocking readline, then you can probably do. code:
|
# ? Dec 3, 2014 14:54 |
|
Edison was a dick posted:I probably read far too much into your request, so if you just want to discard the contents of the buffer then do a blocking readline, then you can probably do. Thanks. My concern is that the other process will output data faster than your function will drain it. I really just want to send the stdout over a UDP socket, so I'll see how the performance of just doing a shell redirect to /dev/udp/ip/port is. The receiving process samples the input every 100ms, so I want to drop packets that are received between sample intervals.
|
# ? Dec 3, 2014 19:30 |
|
zergstain posted:Thanks. My concern is that the other process will output data faster than your function will drain it. Then your best option is to do one read of the size of the pipe buffer and hope. You can get the buffer size with socket.getsockopt(fd, socket.SO_RCVBUF). quote:I really just want to send the stdout over a UDP socket, so I'll see how the performance of just doing a shell redirect to /dev/udp/ip/port is. It sounds like what you actually want is a daemon that does the sampling, which only reports the results when there's a client connected, so the daemon might need to run something like the built-in socketserver with UNIX domain sockets.
|
# ? Dec 3, 2014 20:33 |
|
Edison was a dick posted:Then your best option is to do one read of the size of the pipe buffer and hope. So one thing sends output to the daemon and then the client connects to the daemon? Basically I need to send the output of a tool that only runs on Linux to a program that only runs on Windows. After doing some post-processing. Meaning my bash redirection is a no go.
|
# ? Dec 3, 2014 22:26 |
|
zergstain posted:So one thing sends output to the daemon and then the client connects to the daemon?[quote] How does the Windows program receive its input? What does it do with it? Can it take a stream?
|
# ? Dec 3, 2014 23:07 |
|
Edison was a dick posted:How does the Windows program receive its input? What does it do with it? Can it take a stream? Currently through a UDP port. It needs to be pretty close to real time, so I don't think TCP is what I want. It takes data from multiple devices, and outputs them to CSV. Most of that functionality I added myself. I moved the recv() call to a new thread today. It just overwrites the same buffer over and over, and the sampler thread gets whatever happens to be there when it comes around. With a Critical Section to prevent data corruption of course.
|
# ? Dec 4, 2014 01:02 |
|
I want to scan through the output of a linux command to pull out a specific line which has a string in it. If I use subprocess.check_output I can use re.findall to to show that string in the output, but I want to be able to add the whole line as a variable. I'm thinking I could save each line into a list and search each one, then save that into a variable, but not sure how to do that. I think it's due to the object type check_output is creating, but I'm not 100% Any thoughts?
|
# ? Dec 6, 2014 02:55 |
|
Try this:Python code:
QuarkJets fucked around with this message at 03:51 on Dec 6, 2014 |
# ? Dec 6, 2014 03:49 |
|
I'm just finishing up my first-ever programming class. I couldn't find anything specifically forbidding homework help requests...if I am doing wrong, I will delete this with my apologies. Assume there is a variable , mp_affiliation , that is associated with a dictionary that maps the names of parliament members to party affiliations, associate with the variable party_size a dictionary that maps party names to the number of members they have. Some of the test data MPL uses: mp_affiliation={'Dodds, Mr Nigel':'Democratic Unionist', 'Davies, Dai':'Independent', 'Robinson, Mrs Iris':'Democratic Unionist', 'Bacon, Mr Richard':'Conservative', 'Binley, Mr Brian':'Conservative'} The code I have so far (probably too many comments, but my instructor loves these) pre:#Initializing the party_size dictionary party_size={} #Looking at each entry in the mp_affiliation dictionary. for n in mp_affiliation: #If the party isn't in the party_size dictionary yet, I want to add it as a key with a value of one. if n not in party_size: party_size[mp_affiliation[n]]=1 #If it is, then I want to increase that value by one. I'm pretty sure this is the part that doesn't work. else: party_size[mp_affiliation[n]]=(party_size[mp_affiliation[n]]+1) My current code's output: party_size={'Independent': 1, 'Conservative':1, 'Democratic Unionist': 1} Coin fucked around with this message at 06:13 on Dec 6, 2014 |
# ? Dec 6, 2014 06:03 |
|
What is an elegant way to rewrite a file as I go through it? I am searching a text file line by line and if I find a pattern match, I want to re-write that entire line. Right now, my script works by writing all the contents to a new file, then deleting the old file and renaming the new file to the old file name. Exhausting, I know. But it works. Any suggestions on how I should improve it?code:
|
# ? Dec 6, 2014 06:15 |
|
Coin posted:I'm just finishing up my first-ever programming class. I couldn't find anything specifically forbidding homework help requests...if I am doing wrong, I will delete this with my apologies. Some general tips: Instead of "a = a + 1" you can often use the in-place operator: "a += 1" If you need both the key and the value when iterating over a dictionary, you can use the .items() method: "for name, party in mp_affiliation.items():" If you're using PyCharm (which is a great Python IDE and has a free community edition), you can step through your code line for line in the debugger and check that it is working as you expect.
|
# ? Dec 6, 2014 07:28 |
|
Hughmoris posted:What is an elegant way to rewrite a file as I go through it? I am searching a text file line by line and if I find a pattern match, I want to re-write that entire line. Right now, my script works by writing all the contents to a new file, then deleting the old file and renaming the new file to the old file name. Exhausting, I know. But it works. Any suggestions on how I should improve it? You could edit it in-place, but you may wish you didn't, as if your new line is longer or shorter than the old one, you need to rewrite everything after it too. Plus it can easily get corrupted by concurrent access. With the "write new copy" then "rename into place" one of the writes will be successful. If you have a posix compliant filesystem, you can rename a file on top of another, so you don't ever have a missing file there. quote:
If you want to do this with a bit less code, you can use the fileinput module from the standard library. code:
It also loses the nice property of having a fully written file in place at all times, since it does the rename at the start of processing, rather than after processing, but if you don't anticipate concurrent access then it's probably ok.
|
# ? Dec 6, 2014 13:15 |
|
Coin posted:I'm just finishing up my first-ever programming class. I couldn't find anything specifically forbidding homework help requests...if I am doing wrong, I will delete this with my apologies. This should do it. Python code:
The problem with your code is that you're looping through ministers instead of parties. If you replace the original for loop with for n in mp_affiliation.values():, and every instance of [mp_affiliation[n]] with [n], your original code will work. Note: you can use the [code=python] tag instead of [pre] to get some syntax highlighting in your posts. Dominoes fucked around with this message at 14:04 on Dec 6, 2014 |
# ? Dec 6, 2014 13:36 |
|
You should at least take the repetitive list creation out of the comprehension.
|
# ? Dec 6, 2014 14:42 |
|
What do you mean?
|
# ? Dec 6, 2014 14:47 |
|
Coin posted:I'm just finishing up my first-ever programming class. I couldn't find anything specifically forbidding homework help requests...if I am doing wrong, I will delete this with my apologies. Aag those comments.
|
# ? Dec 6, 2014 14:50 |
|
Python code:
|
# ? Dec 6, 2014 17:09 |
|
SurgicalOntologist posted:
Going to have to look through that module. functools and itertools are neat too.
|
# ? Dec 6, 2014 17:17 |
|
Dominoes posted:What do you mean? parties is already a list, so list(parties) in the dictionary comprehension doesn't do anything
|
# ? Dec 6, 2014 20:01 |
|
QuarkJets posted:parties is already a list, so list(parties) in the dictionary comprehension doesn't do anything Python code:
vv yea that works better. Dominoes fucked around with this message at 20:19 on Dec 6, 2014 |
# ? Dec 6, 2014 20:10 |
|
QuarkJets posted:parties is already a list, so list(parties) in the dictionary comprehension doesn't do anything Dominoes posted:If you're using Python 2, you can skip the list() step, because .values() returns a list in 2, instead of an iterator, which count() doesn't work on. But it still has a problem in Python 3 in that it recreates the list once for every party. Just do it once on the first line: Python code:
|
# ? Dec 6, 2014 20:11 |
|
EAT THE EGGS RICOLA posted:Aag those comments. Are they bad for reasons other than being unnecessary? Dominoes posted:
I did this and it worked (unsurprisingly) Thanks! SurgicalOntologist posted:
I guess not technically, but I feel like correcting my code and sticking with my original algorithm is probably better academic practice. I'll remember that for a personal project/pipe dream I have planned for break, though!
|
# ? Dec 7, 2014 01:20 |
|
Coin posted:Are they bad for reasons other than being unnecessary? Comments that state what code does are bad. They're unnecessary, they can be misleading when you change the code but forget to update the comments, and they clutter the code with redundancy. Aim instead to write code that is itself readable (Python's PEP 8 provides a helpful guide here) and use comments to clarify intent when it's not clear from the code itself.
|
# ? Dec 7, 2014 01:49 |
|
Moddington posted:Comments that state what code does are bad. They're unnecessary, they can be misleading when you change the code but forget to update the comments, and they clutter the code with redundancy. Aim instead to write code that is itself readable (Python's PEP 8 provides a helpful guide here) and use comments to clarify intent when it's not clear from the code itself. Yeah, I've heard this before. However, our instructor teaches us to "code our comments": basically we write everything out in plain English before actually coding it. I can see why this might not be helpful in a production environment, but I'd be marked down for not having that stuff.
|
# ? Dec 7, 2014 01:57 |
|
It does at least makes some sense in an academic setting, especially when you're learning how to read the code in the first place.
|
# ? Dec 7, 2014 02:09 |
|
Coin posted:I did this and it worked (unsurprisingly) Thanks!
|
# ? Dec 7, 2014 15:48 |
|
hosed this post up, wrong thread
|
# ? Dec 7, 2014 17:32 |
|
|
# ? Jun 2, 2024 22:59 |
|
Edison was a dick posted:You could edit it in-place, but you may wish you didn't, as if your new line is longer or shorter than the old one, you need to rewrite everything after it too. Plus it can easily get corrupted by concurrent access. Thanks for this. I'm not knowledgeable enough about the filesystem to say how it would behave with concurrent access. I think I'll stick with what I have since its functional, if ugly.
|
# ? Dec 8, 2014 01:31 |