« first day (1704 days earlier)      last day (3258 days later) » 

Can anyone tell me why the below code does not output print 'HELLO'?
import re
str = "print('HELLO')"
re.sub("^print\(([^)])\)", r'print \1' , str)
print(str)`
 
re.sub does not mutate the existing str string. You have to assign the result somewhere.
Ex. new_str = re.sub(...)`
 
I see. I did str = re.sub("^print\(([^)])\)", r'print \1' , str) but it still gives the output print('HELLO') and not print 'HELLO'
 
(in fact, strings are immutable, so no function can ever mutate one. All functions that perform some kind of operation on a string will return a brand new value)
I guess your regex doesn't match the contents of your string properly.
Ah, you're missing a star. As-is, it only matches if the data inside print(...) is exactly one non-paren character.
 
It won't match
 
1:11 PM
Try str = re.sub("^print\(([^)]*)\)", r'print \1' , str)
 
GOD DAMNIT KEVIN
 
lolololol
 
Do you have a camera in my office?!
 
No, we merely operate on similar wavelengths :-)
 
The council that must not be named has cameras everywhere :p
 
1:13 PM
Why does round(2.5) return 2 instead of 3?
 
That did the trick. Thanks @Kevin
 
@afonsomatos Perhaps the float you're rounding is actually 2.49999999999999, and it only displays as 2.5 when you print it.
 
5/2 => 2.5
round(5/2) => 2 ?
 
Rounding actual 2.5 works on my machine:
>>> round(2.5)
3.0
 
I tried python 2, it works
but python 3 doesn't
 
1:14 PM
It's a nifty little tool
 
If you're literally doing round(5/2), then remember that 5/2 == 2 in Python 2.7, and 5/2 == 2.5 in 3.X.
 
@IntrepidBrit , Thanks. That tool is awesome!
 
because 2.7 performs integer-only division by default
 
@Kevin So why does round 2 is equal to 3?
 
It doesn't on my machine.
>>> round(5/2)
2.0
(in 2.7)
 
1:17 PM
>>> round(5/2)
2
(in 3.4)
 
buh, probably a dumb question, but how do you match the beginning of a word to an endline?
 
startwith() and endswith() ?
*startswith()
 
In regex, trying to do it with :%s/
 
^.*(\r\n|\n) ?
 
@afonsomatos In 2.7, when two integers are equally far from the given value, round moves chooses the number farther from zero. In 3.x, it chooses the even integer.
This explains why round(2.5) equals 3 in 2.7, and 2 in 3.X.
 
1:22 PM
So how can I change the behaviour?
So round(2.5) gives me 3 ?
 
Now I got it... I thought that [^)] meant "scan everything until a )". It is actually "scan a single character which is not ). Does that mean that if the first character is ), [^)] will skip it and will match the next character (if it is not a ))? @Kevin
 
@afonsomatos you can use int(2.5) to get 2 and round(2.5) to get 3
 
@sharpshadow round(2.5) doesn't give 3 in python3
 
and what gives int(2.5) in py3 ?
 
If I want it to round up when it's .5 how do I do it?
 
1:24 PM
@afonsomatos I guess you could write your own version of round:
def special_round(a):
    low = int(a)
    high = low+1
    if abs(high-a) <= abs(low-a):
        return high
    else:
        return low
(not tested, use at own risk, YMMV, offer void in Minnesota)
 
@CoolGuy You can use the tool I showed you to work it out :)
 
Nevermind. Yeah I tried it there
 
you can also use math.ceil() to round up
 
@CoolGuy [^)] means "match exactly one character that isn't ')'". So if the first character is ")", your regex won't match at all.
 
1:33 PM
Today I am trying to determine if there's a special name for the curve that contains all possible values of C for a triangle ABC whose A and B points are known and whose BCA angle is known.
 
Aren't there only 2 values?
</mathsnoob>
(I'm not doing an impression; that is me :))
 
Nah, one known side and one known angle isn't quite sufficient to narrow down the rest that far
 
@Kevin Trevor
 
Hm can't see it
If you also accept ACB then I guess it's four values, but beyond that I don't get it :)
 
Ex. Suppose AB is 1 and angle ACB is 90. There are an infinite number of right triangles with hypotenuse 1.
Not sure if I'm using the right terminology... By "angle ACB" I mean "the angle between the two lines AC and BC"
 
1:41 PM
@Kevin That's what I was taught on this side of the pond too (for naming triangle ACB)
 
Yeah sorry my brain's only waking up now
But then are you sure that points A and B are known, as opposed to the length AB is known?
Oh wait; is this not in 2D space?
 
I'm not so much concerned about the position of the triangle as much as the shape. So we can assume A is at the origin and B is at (1,0) without loss of generality
This is in 2d. But it would be easy to extend into 3d; if C traces out a trevor in 2d, then it carves out a torus with a trevor cross-section in 3d.
 
I am now smart enough to understand your question
Oh sorry, I get it now.
 
Definitely didn't star that comment
 
My awful trevor-drawing program is complete. Looks like a circle.
 
1:54 PM
Hahaha
You actually had me pulling out some paper scribbling to work it out
 
Ok, trevor is a circle iff "given a semicircle terminated by chord AB, for every point C on the semicircle, angle ACB is the same regardless of C" is true.
 
When you say it's terminated by, do you mean the line AB is the diameter of the circle, and the "plane" of intersection?
(and/or the base of the semi-circle)?
 
By "chord" I mean "any line segment that intersects a circle at two points" so you'd form the semicircle by drawing a partial circle and connecting the ends with a straight line
Not necessarily directly through the center of the circle.
 
Gotcha
 
If you do draw the chord straight through the center, it looks like angle ACB is always a right angle
 
2:01 PM
Aye, it has to be
 
Triangles and circles are interrelated. Triangles are the shape of pyramids. Circles are the shape of eyes. ILLUMINATI CONFIRMED.
 
But if you don't draw a chord through the centre of the circle, then it wouldn't be a semi-circle
(otherwise it would be a segment)
 
So basically the whole reason I'm doing this is to see if there's an easy solution to Calculating object position in 3D space. for each triangle ABP, BCP, and CAP, you draw a 3d trevor-torus and try to find the intersection of the three surfaces.
 
@Kevin E3 started today. Triangles have 3 side. There are 3 major consoles. ILLUMINATI Half Life 3 confirmed.
 
God damnit Kevin. I've just written a function called test_semi_circle() instead of convert_temperature_from_pin_voltage(). I hope you're happy
@Kevin It's an intriguing question
 
2:06 PM
So basically, are the angles of the green, blue, and mustard angles all the same?
They look the same-ish.
 
They are
 
@Kevin Yes, and if AB is a diameter, they are right angles. Also, if you connect AB to the centre, the angle at the centre is exactly double that at the circumference (which if you think about it also shows you that C1, C2 and C3 must be the same)
iirc
 
I strongly expect that this is a well known property of chords, and I just don't have the google-fu to find the name of the proof or whatever.
 
hmm - I'm a bit pushed at the minute, but I'd expect something on the front page of Google "circle angle theorems" to give you it
 
Hey, they did unstar some of the starred comments from yesterday.. davidism I think
 
2:14 PM
This page calls it the "inscribed angle" and implies that it's invariant with the actual position of C
 
I reckon about half my stars get cancelled when America comes on line. I'll put it down to an incongruent sense of humour. Or humor.
 
In geometry, an inscribed angle is the angle formed in the interior of a circle when two secant lines (or, in a degenerate case, when one secant line and one tangent line of that circle) intersect on the circle. It can also be defined as the angle subtended at a point on the circle by two given points on the circle Equivalently, an inscribed angle is defined by two chords of the circle sharing an endpoint. The inscribed angle theorem relates the measure of an inscribed angle to that of the central angle subtending the same arc. == Theorem == === Statement === The inscribed angle theore...
 
@Dracunos Low ranking stars tend go nova and forms other starts. It's the way of the world stellar interaction
 
incongruent. topical pun.
 
two part question.. Is America one of the mods in this chat? And, did you just say reckon?
 
2:16 PM
@Kevin I do my best
 
@afonsomatos sorted returns a new sorted list, not the original list. In-place, self-mutating operations in Python tend to return None as a type of internal consistency. It's Python's way of telling you that you've changed the original object.
 
@JanDvorak Oh MemoryError it's like stackoverflow in javascript
@AaronHall ok got it
 
@Dracunos No and yes
 
I think there's a ... puts on sunglasses ... fault in our stars
 
groans
 
2:17 PM
> Corollary (Inscribed Angles Conjecture II ): In a circle, two inscribed angles with the same intercepted arc are congruent.
All right, that proves it to my satisfaction. Trevor is a semicircle.
 
Better than being a square
 
what's the polite and explainable way to tell an intern "don't do that, that's a bad idea that will come back to bite you in the future"?
 
Respond like a good stack post.. Really long, with lots of bold and title formatting, and explanations and examples and markup
 
Construct a prototype that uses the wrong approach, and show how it fails horribly.
Yes, this will take a lot of work. Polite, comprehensible, easy: pick two.
 
Actually, I was being facetious, but giving a real world example is probably the best way that will actually stick
 
2:26 PM
coffee
 
I prefer to take a leaf out of Matthew Hopkins' book
ducking & swimming tests, and if he doesn't repent, to the gallows!
 
2:40 PM
2k more rep and I get moderator tools. Score. "Access 'trusted user' tools - Expanded editing, deletion and undeletion privileges"
 
You also get a fearsome battle aura, which is nice.
(but expect some sleepless nights until you figure out where the brightness settings are)
 
DSM
Morning cabbage for all.
 
What is it with documentation that spans the entire screen?
It's frickin' unreadable.
 
userstyle it
 
Also, black text on white? What are they thinking?
 
2:43 PM
This place gets popular in the mornings on work days. How many of you are at work right now?
 
I am.
 
I am.
 
Remember to account for differing time zones.
It's a quarter til 11 AM here in EST, but for all I know it's 8 PM in Europe
 
@AaronHall what? Also, that can be userstyled
 
Time zones shmime zones
 
2:45 PM
Yeah, I've been using "stylish" plugin for firefox
what do you use?
 
People still use firefox like me? I thought everyone was moving to Chrome
 
Firefox has the handy plugins. Zotero is the killer app for me.
 
I'm using Stylish for Chrome
 
I like the plugins, but every time I download even a couple, firefox eventually gets so slow I have to start over
 
or rather, I have it installed but never actually change the design of anything
 
2:47 PM
@Kevin I just assume it's always 8 pm in Europe
Even when it's 8 pm here.
 
@IntrepidBrit I have defined mysql db
 
@Anuj would it be possible to see the exact configuration of the DATABASES list from settings.py?
 
@limelights I'm figure out it.And I will delete this post soon.Thanks! — 大易归真 2 hours ago
stackoverflow.com/q/30868148/400617 needs mcve, plus the op is going to delete it soon?
 
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'bumper',
    'USER': 'DB_USER',
    'PASSWORD': 'DB_PASSWORD',
    'HOST': 'localhost'
 
is that default?
 
2:52 PM
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'bumper',
    'USER': 'DB_USER',
    'PASSWORD': 'DB_PASSWORD',
    'HOST': 'localhost',
    'PORT': '3306',
}
 
I'm interpreting "I'm figure out it" as "I figured it out". So he'll either self-answer or delete soon.
 
yes that's default
 
@Anuj Aye okay. Some folks have reported that using default for the engine definition fixed the error message you had
Okay, I'm spent until I actually get around to updating Django myself
 
@AaronHall Love Zotero
 
okay thanks. So far I have found that the solution is to remove south
 
2:56 PM
But a lot of good people live down there.
 
I know, killer app, right?
Python assertion followed by a Question: When defining an __eq__ and __ne__, one should define the __ne__ in terms of the other. But do you use the == operator to do so, or the .__eq__ method? What do you gain or lose for doing one over the other?
 
I'm inclined to use ==. Justification forthcoming...
 
ditto, but why?
 
Whoops, surprise meeting is coming up so I'll have to be brief. I like == better because (IIRC) when x.__eq__(y) returns NotImplemented, then x == y will fall back to testing y.__eq__(x). So defining __ne__ as return not (self == other) will incorporate that fallback behavior for you.
 
Yeah, my instincts were right, but my specific reasoning was wrong.
 
3:30 PM
Is there anything like map but that mutates?
 
The above link is now improved with direct link to the documentation. Sad that the accepted answer is wrong.
 
@afonsomatos no, afaik
 
In Python, the general principle is to use the higher level functions/operations so you get more indirection, with the side effect of greater readability.
 
@afonsomatos Assuming you mean an in-place variant on map, take a look at stackoverflow.com/questions/3000461/python-map-in-place and stackoverflow.com/questions/4148375/…
 
So what do we do when accepted answers are wrong?
 
3:39 PM
Leave a comment to the OP
 
Downvote, comment, mention them here if they're particularly bad and you want to call in an air strike.
 
5000 views over 4 years...
 
I'd say not. Can see the point that you would make that it is wrong - and indeed encourage you to comment asking the OP to give a scenario when x==y and x!=y, but the main point (define both __eq__ and __ne__) is right and I think a downvote (should you wish) is enough so i wouldn't be participating in any airstrike.
 
I downvoted.
 
Particularly as "The truth of x==y does not imply that x!=y is false" is a quote from the documentation...
unless I'm missing something else that makes it obviously wrong?
 
3:44 PM
The question is not whether to define both, but whether to define __ne__ in terms of __eq__, and the accepted answer is flat wrong.
 
ah. What am I missing?
 
I can see that's better (and have upvoted), but can't see why it makes the accepted one wrong.
maybe my logic is failing me, it has been a long day...
 
He's telling the questioner that his code is fine. It's not fine.
 
What's the point you're trying to make with negation_of_equals and not_equals?
 
3:52 PM
not (a == b)
=== (should be equivalent to)
a != b
 
right, but did anyone say it didn't? I don't see how it's relevant to the answer, it and the total ordering just seem to be plunked in there
they don't seem to relate back to the question
the point you're making with NotImplemented is valid, I suppose, because of reflection, but that will still work as well if __ne__ uses __eq__, as long as __eq__ would raise
 
Updated the answer: That is, both of the above functions should always return the same result. But this is dependent on the programmer, as Python itself does not automatically implement any operation in terms of another.
 
Yeah - I guess the words "perfectly fine" are wrong. The strong point you're trying to make is that if you get __eq__ is notimplemented, it is interpreted as truthy, right? So not(x.__eq__(y)) is false if x.__eq__(y)` is NotImplemented. whereas not(x == y) might be true?
I don't get the reason for including the total ordering bit - although I'm not at all familiar with the documentation of it.
 
returning NotImplemented from the comparison methods is special, the value isn't actually seen directly (I think)
If none of the fallbacks are implemented, it raises NotImplementedError eventually
 
I'm referencing the source code for total ordering
 
3:56 PM
Where it uses ==?
 
anyways, I'll address further comments when I get back from lunch, which I'm late to.
 
DSM
I'm coming in to this late, but I'm confused. Could someone give an example where using == instead of __eq__ changes the behaviour?
 
You should probably say something like "the example for total_ordering uses =="
@DSM yeah, I'm not quite getting it either.
 
I think an outline example where using the OP code fails, but using the proposed definition for __ne__ works would be very useful and shortcut a lot of this debate
pace Zero and Aaron, who I'm sure see it as an education rather than a debate
 
Why isn't L.index(value, [start, [stop]]) specified in the documentation? It only says L.index(value)
 
4:08 PM
What page are you looking at?
 
DSM
@afonsomatos: that's the array object. Its index method doesn't have the optional arguments.
 
@DSM Oh so where is the list? I can't find list.index
 
DSM
That said, start and stop are missing from elsewhere in the docs, where they should be, for list.index.
(See here for example.)
What it says isn't strictly wrong, but is definitely incomplete.
 
It's right there in the docs for the type
 
4:12 PM
Was about to link that
 
> s.index(x[, i[, j]]) index of the first occurrence of x in s (at or after index i and before index j)
 
Does SQLAlchemy have a concept similar to collection-hooks?
 
What's collection hooks?
@corvid for your earlier question, postgresql has the ltree module, or there's a bunch of examples of different structures in the sqlalchemy examples
 
Ah thank you davidism. Collection hooks, as in "after you insert into this table, do x to another table". Mostly using it for a "feed" on users
 
Yeah, there's the whole event system: docs.sqlalchemy.org/en/rel_1_0/orm/events.html
 
4:16 PM
OK - I think I have a test case - is this what you are highlighting @AaronHall? dpaste.com/060RHR5. The nasty is for class C subclassing the OP's code, right?
 
feelin like I don't know a lot about sqlalchemy right now :| seems like that might be a big thing to miss
 
cbg
 
DSM
@JRichardSnape: see, now that makes sense to me. An example which shows varying behavior.
 
@DSM good. It was worth me doing it then clarified it for me and at least one other :) Aaron can feel free to adopt it if he likes.
 
4:28 PM
why?
 
Oh. The tag score script hasn't executed yesterday. :/
 
I see I spent too much time faffing around and @JRichardSnape got an example first, but: bitbucket.org/snippets/schesis/4k4xb
 
4:58 PM
@ZeroPiraeus to make it clearer, what are the "correct" outputs vs the actual outputs?
 
generally speaking, when do long chains of if/elses start to bottleneck?
I am using a lot of them in order to have an update hook
 
5:33 PM
@JRichardSnape Back from lunch. Yes I might use that. Had to read it on my tablet since my firm blocks just about all paste sites. Github works, though...
 
user559633
@corvid what do you mean by bottleneck?
 
ok guys how can I create a contiguous list out of this file
http://codepad.org/Gy7HSCoK

@XXXX is an address in hex
followed by some values
I need to fill all empty gaps with FF
expected output ["A1","A2","A3","FF","FF","C1","C2","C3","FF","FF","FF",AC 12 "AC","33","DE","FC","A3"]
there is no guarantee that the addresses will be in order ....

the way im currently acheiving this is ugly and gross
is the correct sample input file
 
@corvid Bottlenecking isn't really something you should worry about. It's a readability issue far before that.
Think of it this way... let's say you had a 50-chain of if-else statements.

That crap would be totally unreadable, but doing a linear search on 50 things is trivial in terms of performance.
 
5:49 PM
Touche, I am a bit concerned about its unreadability too, it's a pretty ridiculous set of statements. I think I need some kind of safe transversal of object keys
 
@davidism Sorry, was AFK. They're toy classes, so the notion of correctness doesn't apply in the way I think you mean. What the printed results show is that symmetry sometimes fails using not self.__eq__(other) and doesn't with not self == other ... while there are lots of exotic behaviours someone might want for equality, I struggle to think of an example where someone would want x != y to be different from y != x ...
 
What would be appropriate methods of cleaning up a million if/else statements? In general. Sometimes I feel like I'm using too many
 
user559633
@Dracunos probably the delete key or maybe a shortcut if your text editor supports it
 
I've always like notepad++'s regex find and replace.
 
Just buy a new computer.
 
6:01 PM
I can't think of a good example right now
 
DSM
@JoranBeasley: well, how ugly is your current one? Just a zip, a sort, and some padding, no?
 
ehhh sorta ... naw its bad ... I think i know how now ... zen of python
 
DSM
6:17 PM
Actually, come to think of it, you wouldn't even need to sort, only to get the max and min. But doesn't your expected output have one too many FFs in the second group?
 
Hi all, I have written a Python script to fulfil a need of mine. (convinced after looking that there
is no tool). I intend to release it as open-source on GitHub. This is only my second piece of code
in Python. I was wondering if someone could look through the script and be a (kind, kinda)
critic/mentor and see what improvements can I make before releasing it on GitHub. I have already made one round of improvements based on SO vaultah's suggestions.

The script does this : Look for a windows network share. If it's up, look for a file. If found, look
 
DSM
@WhirlMind: #1, use four spaces for indentation instead of tabs. ;-) (obligatory image link).
 
I'm gonna bring tabs back in style, just you wait
 
Yes, I read the PEP8 twice... I chose not to follow, for now, the tabs-n-spaces-thingy. It
will take time for me to adapt, I hate typing 4 spaces in succession instead of one key. May be I'll do an automated find-replace in Notepad++ But yes, suggestion appreciated and noted..
 
That's one of the few pep8 things that I've heard of causing actual issues
Most editors, npp included, have tab settings, simply check the tab spaces box and tell it four spaces
 
6:26 PM
@WhirlMind Take a look at this
 
DSM
You have a lot of == True and == False in tests, which is non-idiomatic. Simply use if some_condition: and if not some_condition: instead.
 
Python people are really touchy about tabs
 
Tabs stole my car.
 
Oops. sorry about that True, False. I think I already fixed it.
@Dracunos : Thanks for that, I put that setting just now. Hopefully I'll be a good boy in future. :-)
 
Why are you setting msg to an empty dict and then defining a class and then overwriting msg with an instance of the class?
 
DSM
6:30 PM
Bare exceptions (except Exception) are generally frowned upon.
[Aargh, the tabs are messing up the formatting!]
 
I wanted some dict that works like mydict.myprop instead of mydict['myprop']. Hate too many single quotes. Thats why the subclassing of dict n all that..
 
DSM
Anyway, if X: return True else: return False is just a verbose way of writing return bool(X).
 
@WhirlMind Right, but you're defining the msg variable as an empty dict, never using that value, and then overwriting it.
 
DSM
(If X is already a bool you don't need the call, of course.)
 
@DSM : Okay will note that, the boolie one.
 
6:33 PM
@WhirlMind If you use npp (notepad++) it might be worth looking up in Google 'setting up notepad++ for python'. I found so many useful time saving extensions and settings. I fell in love with npp all over again
 
Yes, I use NPP. Will look those up.
 
Check out the pyNPP plugin.
 
@Morgan : Okay, so, defining the empty dict is redundant ?
 
@WhirlMind Yup.
 
@Morgan: I think I have that plugin already, have to look up to using it.
 
6:36 PM
You're doing

thing = 'foo'
thing = 'bar'
The thing = 'foo' call does nothing.
 
@MorganThrapp : Are you referring to the msg variable ?
 
bleh, anyone here good with mongo? I want to sort an array of a collection but not sort the collection by that array
 
@WhirlMind You don't even have an __init__ on your subclassed dict, so it should really just be msg = DictAsClass()
And why does your load_message take an instance of the class instead of self?
 
@Morgan : Yes, thank you. I changed it and it worked.
 
6:43 PM
You could change that whole class to just
def DictAsClass:
self.stuff = 'foo'
You don't use any dictionary stuff, it's just a data object.
 
DSM
What class are we talking about?
 
His DictAsClass class.
 
The load_msg is a function, not belonging to the class DictAsClass.. It uses an instance of DictAsClass and loads the values... Do you suggest it should be a method of the class itself ?
 
DSM
@Morgan: :-( I can't find it!
 
Oh, I misread it. Yeah, I would just make those all attributes of the class.
@DSM Line 48.
 
6:46 PM
@DSM : Hope you are looking at : dpaste.com/16Y8Q9B
 
DSM
" # determine if application is a script file or frozen exe" ? Am I looking at completely the wrong script?
 
DSM
Ah, okay.
 
@DSM : my bad. that was an earlier link .
 
@WhirlMind You only call the function once and the only parameter it has is the output dictionary.
So just make those all attributes of the class.
 
6:48 PM
okay, got it.. Tomorrow, if I have 10 more messages to be notified , I can go on adding to them as properties of the class, is that okay or not okay as good practice ?
 
Output/modified parameters in general tend to be a bad idea in Python.
@WhirlMind Completely okay.
Personally, I would do them just as constants, but there's nothing inherently wrong with your approach.
 
oic, on output parameters. noted.
 
There's just no need to in python, since you can return multiple values.
 
oh, what did you mean, function can return multiple values, did you mean, it can do many things and return one value ?
 
No. I mean it can return multiple values.
 
DSM
6:52 PM
Well, you could argue that Python only ever returns one value, but that value can be a collection of some kind. :-)
 
You can do return foo, bar, baz
@DSM Well, yeah. :P
 
Oh, collection I knew... the list is news to me... Noted...
 
It's not a list. It's a tuple.
You can also return a list return [foo, bar, baz]
 
Ah, yes.
 
Thinking about the power of passing mutable objects around in methods gets me all giddy
 
6:57 PM
Dracun : Are you referring to the msg object ?
 
Meanwhile there is a lisp programmer somewhere in the world getting excited about the power of immutability. It all evens out.
 
DSM
Yeah, our functional friends are warning that the giddiness is a warning symptom..
 

« first day (1704 days earlier)      last day (3258 days later) »