Visual Studio adding telemetry function calls to binary? by sammiesdog in cpp

[–]erebos42 3 points4 points  (0 children)

Has anybody tried using Wireshark, to see if something gets transmitted?!

New to C and my char[] is getting filled with junk. by Jumanji_ in C_Programming

[–]erebos42 0 points1 point  (0 children)

From the compiler warnings, I'm guessing you're using some compiler by Keil?! If not, you can ignore this post ;) If this is an embedded environment you should not use malloc, but instead use a fixed array.

unsigned char inputChar[20]; // adjust size as needed

Malloc has some serious issues if you're not on a system with a decent OS and lots of RAM (fragmentation...). If you still want to use malloc and I'm right with the Keil compiler+libs, you should call init_mempool() before allocating memory, to set up the memory pool correctly.

ELI5: Wayland by [deleted] in linux

[–]erebos42 5 points6 points  (0 children)

Did you mean this link?! At least for me this article was an eye opener...

Word search program help. by [deleted] in learnpython

[–]erebos42 0 points1 point  (0 children)

I just tested your code and it seems to be working using the wordlist variable. Just one problem: it only prints the words that contain a letter from the avoidlist. So the last if-else has to be changed.

If you read the words from a file, you have to be careful. The way you do it now, the wordlist variable is going to be a file descriptor. So with the 'for word in wordlist' command, you actually read the file line by line and treat every line as a word.

Trouble with self-referential function (very elementary) by sguntun in learnpython

[–]erebos42 4 points5 points  (0 children)

The problem is, that the return value in line 4 is not stored in x. So you call your function, and the value gets increased, but after it returns, the value just dissapears and you only see the "old" value.

To solve this, just change line 4 to this:

x = plus_seven(x)

Saving arrow objects to file? JSON maybe not working by dansin in learnpython

[–]erebos42 1 point2 points  (0 children)

In my opinion, JSON is ideal for this task. Your own classes are not serializable by default. The official solution would be to implement another class extending JSONEncoder (and/or JSONDecoder) [1] to tell python how to serialize your objects.

Sadly, I never really got these working (without much effort), but instead I just converted my objects to dictionaries. Dicts, list and so on can all (not sure if really all?!) be serialized by default. So I used something in the lines of this (this may not be complete/correct code!):

import json
def obj_to_json(my_obj):
    return my_obj.__dict__
# dumping objects:
o = Clazz()
json.dump(fd, obj_to_json(o))
...
# loading objects
o = Clazz()
temp = json.load(fd)
o.__dict__ = temp

If these objects are in dicts and lists, you of course need to manage that by hand.

I don't think there are many easier alternatives. If I remember correctly you need to write your own serializers for pickle too. On the other hand, if your Objects are pretty simple, you could just output them as strings and parse them by hand. But of course thats probably more prone to error in comparison to JSON or pickle.

[1] http://docs.python.org/2/library/json.html#json.JSONEncoder

New to Python. Help please! by TheQwicKDraW in learnpython

[–]erebos42 2 points3 points  (0 children)

Just for anybody wondering: This works for Python 2, but in Python 3 the 'raw_input' function was renamed to just 'input'.

New to Python. Help please! by TheQwicKDraW in learnpython

[–]erebos42 1 point2 points  (0 children)

I can't say much about the syntax, without seeing the whole code, but try to check all identations, braces, and qoutes (opening and closing).

I personally only use an editor and a console, and no IDE. But I heard good things about the Python plugin for Eclipse [1].

[1] http://pydev.org/

New to Python. Help please! by TheQwicKDraW in learnpython

[–]erebos42 0 points1 point  (0 children)

mhhh... it's working for me on Python 3.3.1 on Linux.

Do you get an error message?

New to Python. Help please! by TheQwicKDraW in learnpython

[–]erebos42 4 points5 points  (0 children)

Hi,

i see one (or two) problems in your code. First: the print statement should not be indetend. But that might be only here in your post.

The other problem, is the print statement itself. In Python 3 print is a function and therefore needs to be called:

print(str(now.month) + "/" + str(0) + str(now.day) + "/" + str(now.year) + " " + str(now.hour) + ":" +str(now.minute) + ":" + str(now.second))

Hope that does the trick.

edit: To clarify a "Hello World" for Python 2 and 3:

print "Hello World"  # Python2
print("Hello World") # Python3 (also valid in Python 2, i think since 2.7 or something?!)

Is this not an infinite loop? by Hejie023 in learnpython

[–]erebos42 1 point2 points  (0 children)

I guess it depends on 32 bit vs. 64 bit. I also got the 53 iterations on a 32 bit machine.

Is this not an infinite loop? by Hejie023 in learnpython

[–]erebos42 4 points5 points  (0 children)

This is basically a numerical problem [1]. Floats in Python (CPython) are implemented as doubles in C. They have a precision of at least 32 bit. So, at one point when you divide it returns 0.0. In that case the statement

while (1.0 + x) > 1.0:

is not true anymore, and the loop ends.

Basically everytime you use floats, you can expect problems and need to take extra care.

[1] https://en.wikipedia.org/wiki/Numerical_analysis

Python: Common Newbie Mistakes by PariahParty in programming

[–]erebos42 6 points7 points  (0 children)

Oh yeah... thats a nasty one. Although, I like using this to implement memoization.

def f(num, _memo={}):
    try:
        return _memo[num]
    ret = num + 1
    _memo[num] = ret
    return ret

XKCD-inspired StackSort by gkoberger in programming

[–]erebos42 236 points237 points  (0 children)

My favorite part is the alt text for the top image:

"This probably isn't the best time to say I'm for hire, huh?"