« first day (3743 days earlier)      last day (1188 days later) » 

1:02 AM
Hi, I have a very very basic beginner's question about python if anyone is able to answer it....
 
@PortMadeleineCrumpet hello. Just go ahead and ask your question.
 
 
6 hours later…
7:16 AM
cbg people
 
7:59 AM
Cbg.
 
8:21 AM
What would we do if this poorly written softwares don't exist :D
 
8:53 AM
Then we'd write some. It's what we do
 
9:25 AM
morning cbg
@ParitoshSingh So, how's it going the day after getting drunk on power? :P
 
9:58 AM
@inspectorG4dget There's some base of knowledge of representing subclass relationships in relational terms, which comes down to whether you represent the base class and subclasses as a) all columns in one table b) common columns in one table and subclass columns in separate tables, each in an optional 1:1 relationship with the superclass, or c) each class represented as a separate table containing both common columns and those specific to the subclass.
@inspectorG4dget I don't believe it's possible for referential integrity to impose such a constraint on normalised relations.
@lone_coder renaming variables is a refactoring available in many modern IDEs. I know PyCharm and Wing can do it, and imagine VSCode can also. The effort spent in renaming variables will be an extremely sound investment.
 
@MisterMiyagi Haha. I promptly removed the exec from the code as one of the first things today and pushed that to the repo. The power trip was amazing, but i did not want to be tempted by the dark side any longer! (Worked exceptionally well though, got me what i needed!)
 
Also, @lone_coder, establish a standard set of test data and the results from the program before you start refactoring. That way you can run the program at any time and get some sensible indication of whether you broke it (I'm assuming proper testing is absent).
 
Tl;dr 10/10 would try again :P
hey holdenweb hows it going
 
@ParitoshSingh That's the spirit!
 
@ParitoshSingh Well, thanks. Just yesterday formally transitioned to my new role at work. No more programming for anything but fun!
I guess they must want me to display new incompetences.
 
10:15 AM
@holdenweb Are you an exec after for a long time coder? how many years do you code if it is not a problem for you to answer?
 
/usr/bin/which: no python3 in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin) D:
 
@Alper Started programming in 1967. I've been in management positions, but hands-on coding technical management. Now my responsibilities are other people's recruitment, training and compensation.
 
10:38 AM
@holdenweb You are a legend indeed in this job. Respect!
 
Sadly, age alone does not qualify one for that honorific.
 
@holdenweb I am respecting your working length and patience while walking this hard road.
 
@roganjosh I think it was in this podcast where the CM Nicolas was talking about him using Jupyter for some stuff here in SO
 
@Alper I thought that was called "living".
 
@holdenweb Oh that's awesome, best wishes for the new role!
 
10:56 AM
Thanks! Staring around somewhat glazedly at present.
 
11:17 AM
Hi, I need help with one of my school projects. I'm stuck and not sure what to do next.
 
A little more detail is unlikely to hurt. Where have you got to and what are you stuck on?
 
Sure. you dont have to ask to ask. Could you tell us about the problem and show us what you've got so far?
 
Ok sorry. Well I got a project to find the total green pixels of a given picture. I found a way to do for individual images but not for a bunch of them in one go.
 
user13727121
In list slicing, when I insert multiple elements, for example, odd_num = [1, 3, 9, 11], the code I'm supposed to write is odd_num[2:2] = [5, 7] to place them in between 3 and 9, I want to know how did it still work even if I write it this way odd_num[2:1] instead of odd_num[2:2
 
hm. If you can do it for 1 image, you should be able to loop through all images, and simply run your code that gives it for 1 image, and just collect the results
 
11:25 AM
if I want to save the results for 1 image I should use listdir?
 
@Lobsterman If you have code that processes one image, I'd recommend that you refactor that into a) a function to process the image and return the required result, and b) a single call to that function.
It will then be a (relatively) simple matter to replace the single call with a loop that generates all the images you want and processes them, one at a time.
 
user13727121
it only works when I have a lower index value for the second index, example: num = [11, 3, 4, 8, 9], and I want to fit 5 and 6 in between 4 and 8, I should be doing num[3:3] = [5, 6], writing num[3:2], num[3:1] or num[3:0] works too, why's that?
 
@Lobsterman If you want to find a bunch of image files then the glob built-in library will help you find them by name. I thought from your description you expected a single integer for each image. Is that not the case?
 
Because python's slices are designed to be lenient. can observe the other behaviours that arise when you try to slice outside the index range of the list too
 
Need to calculate each picture of a directory green percentage and create an excel containing set values to be displayed in a geospatial map
import cv2
import numpy as np

img = cv2.imread('Image')

grid_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(20,8))
dimensions = img.shape

# Load image and convert to HSV
im = Image.open('.image').convert('HSV')

# Extract Hue channel and make Numpy array for fast processing
Hue = np.array(im.getchannel('H'))

# Make mask of zeroes in which we will set greens to 1
mask = np.zeros_like(Hue, dtype=np.uint8)

# Set all green pixels to 1
mask[(Hue>80) & (Hue<90)] = 1

print(mask.mean()/100 * area/100)
 
11:32 AM
So, this might very well be a side effect of that design choice (or perhaps intentional, i cannot say one way or another). But slicing is essentially taking a "subsequence" from a sequence. And so, empty slices are a fair game when you try to take a subsequence with parameters such as the ones you passed
 
Here's my codes. So loop means the "if" function? sorry I'm new to python
 
Yeah, basically the slicing arithmetic automatically imposes bounds, ignoring any attempts to address outside the object. So all the slices num[x:x] represent the empty slice starting at index x.
 
@CoreVisional put the indices in a range and check the len of it. That should give you an idea.
 
@Lobsterman Ah, you have a little learning to do. Try the following code interactively:
for s in ['one', 'two', 'three']:
    print(s)
 
Thanks holdenw for taking the effort to explain to me.
 
11:35 AM
'swhy we hang around here. As well as just to hang out.
 
user13727121
that means I only need to worry about the first x if I ever I want to place an element in between the elements in a list?
 
I wouldn't draw that conclusion from the given premiss.
 
@CoreVisional You need to worry about the start index and the length of the slice.
 
user13727121
In my example above, num = [11, 3, 4, 8, 9], if I want to add and place numbers in between 8 and 9, writing it in this way also do no harm right? num[4:0] = [1, 2]
 
I'd say "all subscripts [x:y] where y <= x reference the empty slice starting at index x." But I've spent years learning to think as precisely as I can about programming problems, and might therefore seem pedantic.
 
11:38 AM
@CoreVisional In that example, yes.
But be careful not to overdo it, e.g. it this might fall apart with negative indices and steps.
For example, num[4:-1] = [1, 2] and num[4:0:-1] = [1, 2, 3, 4] will not do what you want.
 
best to actaully write the slice as intended. Since you are going through the effort of figuring out the start index anyways, and you know the start and the end are the same
 
We should also perhaps remind @CoreVisional that list insertions of that nature tend to be inefficient due to the need to reorganise the underlying list's structure (though one might argue that's a CPython-specific issue).
 
Besides, it would be easier to understand the code at a glance if you write num[4:4] instead of num[4:0]
 
+1
Convention is important. [4:4] makes sense without further thought. [4:0] causes the reader a mental stumble.
 
another question about that. So if I wanna do for a directory I just key for s in ['folder/pic/*jpg']:
print(s)
for s in ['folder/pic/*jpg']:
print(s)
 
11:42 AM
@Lobsterman You might want to take a look at pathlib and the Path.glob method.
Python is not a shell, so 'folder/pic/*jpg' is the literal string 'folder/pic/*jpg' including a *.
 
Would that that were the case. That's why I mentioned the glob module, but @MisterMiyagi is more up-to-date than I am. I have old-timer's bad habits :)
 
Only because I'm porting my own, dated Py2 code right now. ^^
 
The docs will probably explain enough about looping for you to get your job done.
 
@MisterMiyagi oh alright
 
so I'm trying to count the amount of continuous repeated character in a string, but not sure how to do it?

I thought of using a variation of the code posted on these links:
https://www.howtosolutions.net/2019/12/python-count-number-of-times-characters-repeat-in-a-string/

https://www.codespeedy.com/count-repeated-substring-in-a-given-string-in-python/

https://www.tutorialspoint.com/python-program-to-find-all-duplicate-characters-in-a-string
etc

But most of these template/implementation don't replace the actual counted character by their count in the original string....not sure how to do
 
user13727121
11:44 AM
So basically, the whole thing goes like this? [start:end] where start refers to the number I want Python to start inserting the elements and end is where it stops?
 
there's one more param, step size. [start: stop: step] That one defaults to +1 if you dont specify it
 
user13727121
@holdenweb I understand it's the convention, it's just that, there's no repercussion for doing it in that way, unlike in @MisterMiyagi's example, there are changes made to the output.
 
@NordineLotfi First you say you want to count, and then you complain that the solutions don't modify the string? What are you actually trying to do?
 
@NordineLotfi sounds like a job for itertools.groupby.
 
user13727121
@ParitoshSingh Haven't learned step yet, but will soon and thanks for telling me :D
 
11:49 AM
@MisterMiyagi Sounds more like a good time to learn about loops and practice problem-solving to me tbh
 
@Aran-Fey I'm trying to count the "continuous" repeated character, so if there "hheelloo" it would replace it with "2222", without grouping later repetition in the same string (eg: helloworld -> 112111111) essentially
and I'm not really complaining, just pointing that i "tried" those implementation but didn't know how to make them do what i mentioned
 
@Aran-Fey learning things from the ground up? Heresy!
FWIW, this looks like a nice job for a generator.
 
Okay, so you can use itertools.groupby for that like MM suggested, but... do you have any idea how to do this without some fancy tools that do all the work for you?
 
One suggestion that i think might be helpful for you. Stop trying to think in terms of "replacing" in the string.
Instead, try to think in terms of "creating/generating" this output from the input
 
@Aran-Fey I'm used to POSIX workflow, so i guess yes? I'm just trying to do it in python since i wanted to /s
 
11:52 AM
Actually, scratch that: Even if you do end up using groupby, do you have any idea what to do with its output?
 
Which is a hopefully not-so-subtle way of saying: make a new list for this output!
 
@Aran-Fey mostly print it out :D later plans to do actual things with the output is beside the point here
 
...so you'll do print(groupby(input_string))?
 
@Aran-Fey i see :O I'll try that, thanks
 
It was a question :/
 
11:54 AM
oh
 
I should probably point out that itertools.groupby is a beast unless you know what it does...
 
@MisterMiyagi welp, guess there no simpler solution?
 
Show me the code that you would write to turn "hheelloo" into "2222". It's okay if it's incomplete. We can fill in the blanks later. What I want to know is, do you have any idea how to get started?
 
@NordineLotfi Nobody said that. In fact someone asked you if you cold think of a simple solution. We need to know where to pitch answers.
 
@NordineLotfi Don't take me wrong, itertools.groupby is simple. But you need to grok some things to "get" it; your current task is perfect for learning them.
 
11:57 AM
@Aran-Fey yeah, I'm mostly stuck on how to make the logic for only printing "continuous" repetition instead of the total repetition of the whole input. I think i already know how to count repeated character "normally" and also how to replace the said chars with the count....
 
So what does your code currently look like?
 
@MisterMiyagi gotcha, yeah i mostly do this as a learning tool too
 
@NordineLotfi Did you check our code formatting guide? As Aran mentioned, you should provide your code; that allows us to nudge you along instead of hammering you with a solution too large to chew at once.
 
import sys
import collections
results = collections.Counter(sys.argv[1])
print(results)
it print this:
Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
using the string "hello" as test
 
That's not quite doing what you want, is it? E.g. does it work for helloworld?
 
12:04 PM
Counter is no help because it doesn't count contiguous occurrences. Counter('aba') and Counter('aab') will produce the same result. Any other ideas?
 
@Aran-Fey hmm, i could look into itertools like MM mentioned?
@MisterMiyagi it does, but doesn't for contiguous occurrences (thanks to Aran-Fey for the correction)
guess I'll look at the docs and try some more stuff hmm
 
@NordineLotfi Sure. So when you get something like [('h', ['h']), ('e', ['e']), ('l', ['l', 'l']), ('o', ['o'])] returned from groupby, what will you do with that return value?
 
replace the contiguous occurrences by their number reprsentation? so ll -> 2, then print that?

Could use the re module for this as i used it a lot before
 
Okay, but how will you do it? Can you write the code for that?
 
@Aran-Fey sure, I'll try :D
 
12:41 PM
Another asker scared off I guess. Somehow I'm getting less efficient. Time to start implementing speedrunning strats
 
1:14 PM
cbg!
 
1:28 PM
cbg
 
Haven't been here in many years.
I hope y'all are doing well.
 
cbg, good to meet you.
 
@holdenweb Steve! It's been ages!
How are you?
 
What desperate circumstance drove you to return? Well, thanks, and you?
 
@Aran-Fey nah, they'll be back
 
1:32 PM
Not much. Just wanted to see how my old friends are doing. This is where my career sorta started, and I learned a lot here. So I always try to come back every now and then.
 
And how's the career going?
 
Pretty good. JetBrains is a good place.
How are your grandkids?
 
2:11 PM
Both well, thanks, but haven't seen them in person for a year now. Partner has acquired two more along the way. Glad JetBrains are treating you well. They have some smart people there.
 
2:38 PM
morning cabbages, folks
 
@holdenweb Indeed they do!
@inspectorG4dget cbg!
 
3:17 PM
good to see you around these parts again, @GamesBrainiac!
 
 
3 hours later…
6:08 PM
wasn't scared at all! Just disconnected because i went afk :D
Your ways of helping is pretty good as it is, believe me.
@AndrasDeak yep
 
anyone know whether an on delete cascade on a foreign key would fire an after deletetrigger? The only reason that I can come up with for why this trigger isn't getting fired is that a cascading delete in fact does not fire after delete triggers. This seems like an obvious feature, so I have to be wrong... right?
 
AAB
6:40 PM
Hi all,
 
(changes name to "all")
Hiii!
 
 
2 hours later…
8:29 PM
Say someone is iterating over a list, and as part of it is .append()ing to the end of the list, creating an infinitely large list. At some point Python will crash, but what is that point? Does it take until the list actually fills all memory available to the process, or does either the OS or Python see what's going to happen and kills the process/commits suicide?
 
I'd expect memory to run out. Even more likely: if the system has swap, thrashing stops all meaningful execution
I don't think python has the capacity to estimate that memory will run out. It can raise a MemoryError which can be handled, but again with thrashing that might not be raised
and it's probably up to the OS how it handles OOM things
Note that my thrashing remarks assume an HDD. I suspect thrashing is not so bad with an SSD but I've never had one.
 
I thought that was kind of the point of SSDs. That, and speed.
 
8:44 PM
Thrashing and speed are the same thing, aren't they? :)
I'd be slightly concerned about too many read/writes when swapping, but I'm not sure that fear is founded in reality
 
So, with an SSD, how long would it take to fill up swap on your standard desktop with, say, 16GB RAM and maybe another 100 in swap? What is the data throughput of a 4-core processor these days?
 
the recommendations I've seen for swap are ~1-2x your RAM
and the 2x used to be the recommendation for hybernation support
All of these on debian-like linux. No idea how others and windows do this.
 
the memory would fill up, then the swap space would fill up. When I accidentally did this back in 2009 on whatever ubuntu I had at the time, it took a few seconds for the laptop to stop responding
 
@MattDMo and with pyhon you can normally only fill your memory with a single core
 
ok, good good. I win the argument :)
 
8:52 PM
that'll be one pound
no, wait, this was the full half hour
 
I'll buy a couple rounds of beers
 
 
2 hours later…
10:39 PM
If I pass an N x 2 array to matplotlib.pyplot.plot, it plots 2 lines. How can I set the individual labels for those 2 lines?
 
@user76284 by storing the return value from plt.plot and calling plt.legend with the two-argument form
It's something like plt.legend(list_of_lines, list_of_labels)
 
Perfect, thanks.
 
No problem
 
11:05 PM
btw, found the solution and made it work :o
import itertools

x = 'helloworld'
new = itertools.groupby(x)
new = [len(list(g)) for k,g in new]
print(new)
thanks for y'all help ! @Aran-Fey @MisterMiyagi
also, does anyone know any decent template/project that do TUI (terminal UI) in python, but with well explained code etc?
want to make one but the docs look daunting, so prefer something that already does some stuff so i can learn from it by tinkering
(already looked at curse's code, but wanted something "simpler")
 

« first day (3743 days earlier)      last day (1188 days later) »