/dpt/ - Daily Programming Thread

Old thread: What are you working on Sup Forums?

Other urls found in this thread:

stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python
stefaanlippens.net/python-asynchronous-subprocess-pipe-reading/
pastebin.com/TySRgxye
boreal.aggydaggy.com/programming/2016/05/26/entity-safari.html
twitter.com/NSFWRedditImage

I'm making a game!

My boring ass CSP exam.

Good luck frogposter!

Inductive programming really is the best thing ever.

Me too user.

Stupid fucking frogposter I hope you fail

I KNOW you'll fail

I fucking hate you

die

Is the book any good? any links for the a pdf?

why tho

Cuz he likes gay traps

This week is the one-year anniversary of when I started taking coding classes at my college. I'm a CS major now and I feel like I've finally grasped enough fundamentals to start going my own and doing my own projects.

At this point I have been trying to learn as much low-level programming with C as possible because I like it the most; I might end up becoming a systems engineer.

For fun over winter break, I made a pretty simple cache simulator. It's not incredible but I'm proud of it, seeing as how I am still a beginner to CS.

Can anyone recommend cool projects involving C that perhaps involve hardware or the operating system? I'd like to get some kind of personal project in to even have a chance at an internship.

>traps
>gay

After five months of unemployment I finally have another job...

now all I fear is pure terror because I could be fired at any moment for even the slightest slip-up or sign of disinterest

ohgodohgodohgodohgod

project idea: Dynamic frog-post generator

Work shit with Go and JS.

I decided I'm going to relearn C and Python. Is K&R still the standard for C? What's the new meme for Python?

>k&r
nothing changed
>python
if you need a book to learn python kys

I'm on it.

godspeed lad

I bet you can't even program

Post some code

>you'll cowards don't even program
ftfy

Can you?

im:io

prgrm
{
whspr("h-hello")
}

I'm am learning how to use the subprocess module and a little about multi-threading in python3. I am at a point now where I can run a child process (like a test program in python or play a video with mpv) and capture both stdout and stderr (a lot of examples I have seen only capture stdout and redirect stderr).

stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python
stefaanlippens.net/python-asynchronous-subprocess-pipe-reading/

The above examples are what I used as a reference. Now I have something that works!

pastebin.com/TySRgxye

My question is with the following part of the code:
while tout.is_alive() or terr.is_alive():
while not queue_out.empty():
line = queue_out.get()
text = '*{}'.format(line.decode('utf-8'))
print(text, end='', flush=True)

while not queue_err.empty():
line = queue_err.get()
text = '+{}'.format(line.decode('utf-8'))
print(text, end='', flush=True)

# is a small sleep() needed here?
# time.sleep(.1)

If I run the program as is, my cpu usage jumps to ~26%. If I uncomment the sleep command my cpu usage is almost nothing.

Why does the sleep command result in lower cpu usage?

I'm working on a really large project in C++ and it has a really huge inheritance tree. It has all the usual problems that inheritance brings... So I'm looking into the "composition over inheritance" design pattern. Any Sup Forumsentlemen familiar with this? I've read all the articles I could find in Google and watched all the YouTube videos. I get it, I guess, but everything out there describes the idea at such an oversimplified level that it's not really coming together for me when I try to convert this massive class hierarchy into a bunch of components. Any better sources of information?

Please, stop coding.

...

guess: breaks up cpu usage spikes so they don't register as one big spike

If A inherits from B, instead give A a field storing a B.

what language?!

HTML

not programming

A concept one I've been working on sporadically.

Kill yourself then.

without the sleep your program is frantically checking if tout, terr, queue_out, queue_err are empty or not. the sleep 1 lets your computer rest every time. with the sleep, your program probably checks these about 10000 times a second (or however fast your cpu can go), with the sleep, your program only checks 10 times a second.

gibs now

Yep, that's the summary of everything I've seen so far. That doesn't quite cover it in practice, unfortunately.

Please, start coding before coming to programming threads.

>That doesn't quite cover it in practice, unfortunately.
What the fuck is it missing?

Not much to show 2bh.

>Please, start coding before coming to programming threads.
You're talking to a master. So shut up little code monkey.

Hi guys. I have a question. I'm writing a very low-level but fast byte code interpreter and I'm wondering if anybody has some good PDFs on typed assembly languages? If it helps my VM is using Elf as the container format.

Well, if you've ever tried actually implementing something large with composition, you'll know that it shifts everything from compile-time (inheritance), to run-time. Anything that can move has a "moveable" object and anything that can be drawn has a "visible" object and so on... and you end up with a thousand allocations every time you create something. Performance sucks. And your code becomes very bloated with conditionals being checked on every single object "If this object has a 'moveable' then move it" etc... It's kind of impractical when you try to do something real, as opposed to everyone's examples with three classes "car," "boat," and "plane."

Sounds like the fault of your garbage language where everything is behind a reference and on the heap out of your control.

And null references, etc.

Ok, that makes sense. Thanks.

What kind of "control" are you talking about? In C++ you create things with "new" and you get a pointer and then you delete them with that same pointer. What else do you want the language to allow you to do with them?

>allocations increase
no, the only extra allocations you need are single extra pointer to the composite object
>And your code becomes very bloated with conditionals being checked on every single object "If this object has a 'moveable' then move it"
your interfaces shouldnt be changing, so what's the problem here? dont tell me you're adding isMovable() methods....

>dpt
>almost every post is a random insult or explanation of how I am so great and you are a shit-tard

Why are smug wannabe "programmers" literally the worst people ever?

It isnt, the plain algorithm version is the most up to date and best organized, is java but is very readable java for algorithms. If you know any C derivative it'll be good for you. Tho I feel sedgewick is a bit too hand wavy at some points.

Should I wait until my project actually has some functionality before uploading it to github or is it okay to upload it when all i've done so far is interface design?

In C++ you'd just make the components fields without doing anything else to them. No pointers.

Learning Racket
(define (sum-me values)
(if (empty? values)
0
(+ (first values) (sum-me(rest values)))))

sounds like you're not relying on IoC (and/orSRP and noticeably DIP).

rather you seem to be passing around concrete implementations that do too much and aren't really thinking about the relationships of your objects (is-a vs has-a)

the extra overhead is basically the vtable and 1 extra indirection, but pimpl and crtp can alleviate that

The entire Sup Forums is like this, but as a programmer I do agree that programmers are one of the most obnoxious and arrogant creatures on earth.

you've never met the netsec wannabe hackers..

pls reply desu
wat do u guys do

Never wait to do something that you can do now.

wait until your project becomes usable

I might be misunderstanding, but where do your object's "moveable" and "drawable" and "whatever-able" objects come from if you don't allocate them? Any component-based code I've seen so far allocates these in the constructor of the owner.

No, there aren't necessarily "isMovable()" methods, but in a fully component-based system, you have to check somehow. I just use if/else checks. Even in the simple examples like "car" "boat" and "plane," your main loop is going to have to say "If this object is 'flyable'..." and the car will return false and the plane will return true and so on.

With inheritance, you just say plane->fly() and that's it, no check. You can't call boat->fly() because boat doesn't have that method.

>Never wait to do something that you can do now.

>wait until your project becomes usable

reeeeeeeeeeeee

it doesn't matter, just do work off the master branch and merge (working) changes over. if you expect people to use it then you should also use tags.

but github is just for social/collaborative coding.. so those don't really require a specific set of functionality in a project to work. you could use github for the issue tracker, wiki, and other project management tools which'd help improve feature implementations -- so sooner the better in that regard.

also
>using that cuck service github after trump got elected

And with using cond
(define (sum-me-cond values)
(cond [(empty? values) (0)]
[(+ (first values) (sum-me(rest values)))]))

Is it common practice to use '[' and ']' in the tests?

>using that cuck service github after trump got elected

kek

Using composition instead of inheritance doesn't mean you have to go for a dynamic component-based design.

s/tests/branches

>if you need a book to learn python kys

t. someone who has never written anything of worth.

>yfw

how would you know fucksnot

YES, I'm so glad someone mentioned this... I want to believe you're right, but there isn't ONE example of doing it statically, seriously. Every article and every lecture do it dynamically.

I can kind of vaguely imagine a static component system, but the details of it are difficult.

Yea, of course, you can have a class A and a class B and B can be a static member of A. But how do you build a component-based system around this? I would love to read an example of this. Honestly this would be the best of both worlds. I was just trying to make this work.

boreal.aggydaggy.com/programming/2016/05/26/entity-safari.html
I'm glad you asked.

how are you doing static binding / (or, probably what you mean) static allocation in your hierarchical system?

are you sure you aren't dynamically allocating those subclasses at runtime?

That book is shit.

it sounds like you are changing the interface of your objects. dont do that.

It's C++, that's just the way it works. When you allocate a subclass, you get all the superclasses at the same time. Their constructors get called individually, etc... but the compiler lumps them all into one block of memory and it's just one allocation.

Maybe dynamic vs static is always a big deal, but it definitely can be. If you're allocating thousands of objects, and each of those has to dynamically allocate its components, performance is ruined. Memory allocation is a very expensive operation.

There's no way around it. In a class hierarchy, you have

entity -> vehicle -> flying_vehicle -> plane -> fighter_jet

In a component-based system you only have "entity." The entity either has a "wings" object or it doesn't. Completely different interfaces.

I'm starting with a text adventure, don't want to bit more than I can chew.

Reading this now. Looks like a really good start. At first glance, it looks like the magic bullet is how he's storing everything in groups, like "StandingChars" ... Very clean.

Thanks for this, I think it's exactly what I want. I will read it carefully and find out.

>In a component-based system you only have "entity." The entity either has a "wings" object or it doesn't. Completely different interfaces.
That's not true. With composition you could still have all those different types and you simply change inheritance to adding a field of the previously inherited type.

>days in months are indexed from 1
>months in year are indexed from 0
whoever made java.util.Calendar should gas himself

What are entity component systems like in languages with row polymorphism?

Should I start with SICP?

because there's a lot more to python than "hello world" you retard

Dynamic composition based organizations don't use subtyping at all.

>mfw I have +10k lines of uncommented code to comment

I'm reading that book in it's 1990 edition, I think it's the first one.

Should I read the later ones instead?
Because the code examples reek of K&R and it's distracting as hell.
I get more mileage reading the detailed descriptions.

On that same note, what uses are there for binary search trees other than syntax parsing?

In college I wrote a program for a class to calculate how quickly the processor can do an ADD operation. So a for loop of variable++, divide it out. Then we went on to making it multithreaded. You can expand from there and make your own hardware monitor.

composition over inheritance =/= component-based system. You will still have the same exact types and interfaces, but you go around it differently.

You have a fighter_jet. The fighter jet currently inherits from plane. Instead of inheriting, you just give it an instance of plane and delegate all calls, for example

Plane {
flying_vehicle flying_vehicle
void fly() {
// whatever
}

void_exist() {
flying_vehicle->exist()
}
}

fighter_jet {
plane plane;
void fly {
plane->fly()
}
void exist() {
plane->exist()
}
void shoot() {
//whatever
}
}
So you still have the same types and the same interface, you're just not using inheritance.

Reminder that bad code requires a lot of comments. I like to comment too, but only if i'm explaining some logic that is not obviously in the code.

>new to programming
>have to take a MATLAB class at uni
>had to create a programme that takes 3 numbers, then averages the two largest numbers
>use vector operations
>demonstrator comes along and says it's better to do it in loops
>MATLAB documentation says vector operations are faster

What am I missing here?

mention it and ask him what and why and stop being such a lil bitch

>It works tho

>mfw that's my reasoning and I submitted the programme anyway

>What am I missing here?
He's wrong.

A lot of good game programmers started with a text adventure, good choice.

The main thing to focus on at the stage where you are now is separating your code and data. You're going to want to start out by writing it like this:
print("you are in a room. you can go east or west.");

getkey(k);

if (k == 'w')
print("you went west. now you are in a dark room.";
else
print("you went east. now you are outside.";


That's really bad and you'll learn why very quickly if you go down that road. Store the game's map as some kind of simple database, even just a plain text file is fine. Read in the database and then have your game's main loop look more like this:

while (!game_over)
{
print_current_room();
get_player_input();
move_to_new_room();
game_over = check_if_died();
}

>loop for this
literal autism

>unitards in charge of writing good code

I also have to write its documentation. The problem is that I didn't even write the fucking code. Some guys did some HR project or some bullshit at work and they "don't like documenting", and it falls to me to do this pajeet's job.

reminder that uncommented code isnt actually readable

That's an abuse of composition, though.

You're going from saying "a fighter_jet is a plane" to "a fighter_jet has a plane" and "a plane has a flying vehicle."

It misses the point of using components at all.

Code isn't taxonomy, and it doesn't miss the point of composition. You can use composition more than once, for example, and there's no ambiguity.

Holy fucking shit, that's sounds like a nightmare.
Sometimes is better to no comment at all, because it's dumb and redundant.
For example: >pic related
Both functions do pretty much the same, but the opposite, one is clear enough to guess what it does, and the other one needs comments since it makes other extra operations.