Notifications
Clear all

how to code python

80 Posts
9 Users
10 Likes
20.3 K Views
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robo-pi

Posted by: @robo-pi

I tend to be a lone coder.   I just write code for myself and seldom need to work with other programmers.  But I imagine that when working in a group everyone needs to get on the same page.  I'd most likely need a lot of training to be able to work with other programmers.  I'm just used to doing things my own way.

LOL, I'll leave this one for the "camel case versus hieroglyphics" flame war thread 🙂


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 
Posted by: @frogandtoad

@pugwash

Posted by: @pugwash

"I consider Python as a scripting tool rather than a programming language".

I consider these 3 amigos an operating system ? 

  • grep
  • sed
  • awk

I just love bash scripting. The Terminal is as much ignored as it is misunderstood!


   
frogandtoad reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@byron

Posted by: @byron

You would be expected to include the code for your original class. No cheating now tish tish?

LOL, OK... I will provide an example shortly... watch this space ?

Rather than posting an example to compare languages, is it worth starting a new topic to gather ideas of how the programming workflow for a robot (of reasonable to advanced complexity) should look like?  Think partial AI, self awareness properties, automated actions based on other actions, loose coupling, segregation of duties, etc... something you would expect a real android to perform.

Again, this is not intended to be about the language, but more about the logical program flow and algorithms used in a larger, intelligent project.

Any takers?


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1111
 
Posted by: @frogandtoad

Any takers?

Whoa up lad, "real androids' whatever next.   No, my apparition is to make my 4 wheeled robot be sent a message as to where movement is sensed somewhere in my field and to navigate to said place to growl at possible rabbit incursion.   And due to DIY home improvements I wont be getting back to that project until the end of this year.   Currently I'm knocking tiles off a bathroom walls, its a bit dusty so I take plenty of tea breaks when I peruse any emails and have been keeping an eye on this forum.  I've been taking far too long a tea break as it is. I'm up for code snippets but nowt extra.  And when I get really stuck in to putting the bathroom back together (the wife will surly start nagging me soon ?) my attention to playing with code will be limited let alone projects for advance androids.   

You address this to me, but maybe you will get another buddy to play.  Good luck.


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @frogandtoad

@byron

Posted by: @byron

You would be expected to include the code for your original class. No cheating now tish tish?

LOL, OK... I will provide an example shortly... watch this space ?

Rather than posting an example to compare languages, is it worth starting a new topic to gather ideas of how the programming workflow for a robot (of reasonable to advanced complexity) should look like?  Think partial AI, self awareness properties, automated actions based on other actions, loose coupling, segregation of duties, etc... something you would expect a real android to perform.

Again, this is not intended to be about the language, but more about the logical program flow and algorithms used in a larger, intelligent project.

Any takers?

YES!

This is exactly where I'm at right now.  My robot project is at a point where it's really starting to take off.  At least in terms of a lot of different facets finally "coming together".    In fact, the complexity is getting to a level when I've been thinking of just stopping and taking some time to regroup and draw up some flowcharts.

I have a bad habit of just designing things on the fly right off the top of my head.  But the project has finally gotten to the point where my poor little head is having a hard time containing the entire scope of the all the different algorithms. 

Not to get back to the language wars again, but I'm also writing part of the program in C# on my laptop and two other parts on the robot in C++.  One sketch on the ESP8266 WiFi chip, and the other sketch on the Arduino Mega.  Up till now I've keeping track of all of this in my head.   But now each of these individual programs are themselves becoming multifaceted containing multiple classes within them.

It's time for a plan! ?

This has actually become a serious concern for me just recently.   Because up to this point things were pretty easy to keep track of.  But now they have reached a level of complexity were I really do need to stop and get better organized.   At least in terms of keeping better organized notes if nothing else.  So yeah, a thread where flowcharts and algorithm schemes are shared is a really good idea.  No need to bring any specific code into that thread because most algorithms can be written in any language one chooses to use.

By the way, I wrote a robot fantasy story.  Nothing exciting.  It's only one post long.  But the crux of the story is to expose how the internal programming of a robot actually works.  It's basically a story where the characters are all various different components of the robot program interacting with each other.  I asked Bill for a robot fantasy forum so I could post it there.  But actually it would probably be better posted in the programming section entitled "An Algorithmic Fantasy" ? 

But yeah @frogandtoad that's a great idea!

I wonder where we should post the thread?  We don't really have a sub-forum dedicated to flow charts, algorithms or overall programming structures.   All we have in the programming section is Arduino, Raspberry Pi, C++ and Python.

I could put it in the Artificial Intelligence section?   Just under Artificial Intelligence in general.   My robot will eventually become a self-learning machine.  But it's not there just yet.   What I'm doing now is just creating the foundation for something that will become self-learning a bit later.

In fact I think I will go post my robot fantasy story there since the robot in my little fantasy skit does appear to be quite intelligent, even though it's a pretty simple collection of algorithms.

Yeah, the Artificial Intelligence is where I'll post the fantasy skit.   That seems right.

DroneBot Workshop Robotics Engineer
James


   
BuilderBob reacted
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 

I posted the Algorithmic Fantasy here:

An Algorithmic Fantasy

Not exactly a workable flow chart, but it does convey how a lot of different programs come together to create a single autonomous robot.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2035
 
Posted by: @pugwash

@frogandtoad

I don't think python is hard to learn

Totally agree. It is certainly less cryptic that other languages but then again most of the interpreted languages are close to English. But have the severe speed disadvantage compared to compiled languages,

Probably Python is easy to learn but I found it hard because it was alien to the other languages I used, C++ and BASIC and I had to think differently. What were these tuples, dictionaries and lists about!!  I managed fine without them before.  Now I had to learn yet another way to do exactly the same thing.  For example I already knew how to print a string using BASIC or C++ and I really didn't care to have to learn yet another way of doing it.

So C++ may be harder to learn but I already had learned the basics and dabbled in the more advanced C++ programming techniques and would rather have used what I already knew then have to start again from scratch.  It was hard because I really didn't want to do it however there is no choice as Python is the anointed one when it comes to software for all these cool Raspberry Pi  hardware projects.

As a side note I consider the use of f as a PRINT command to be cryptic.

f"Hello, {name}. You are {age}."

PRINT "Hello, ";name;". You are ";age;"."

cout << "Hello, "<< name << ". You are " << age << "." << endl;


   
frogandtoad reacted
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1111
 
Posted by: @casey

As a side note I consider the use of f as a PRINT command to be cryptic.

f"Hello, {name}. You are {age}."

PRINT "Hello, ";name;". You are ";age;"."

cout << "Hello, "<< name << ". You are " << age << "." << endl;

 

I'm not questioning what you find harder or easier to read, but I do point out that f is not a print command in Python, rather its a format statement.

To print your string and insert the variables name and age you would print it as follows

print(f"Hello, {name}.  You are {age}.")

There are a number of ways to format strings and insert variables in Python and the f before the string is just an instruction the interpreter to find a variable with the name if finds in the curly brackets and place their contents in the string at the point where they are placed.   And thats what I find so appealing about f strings, no multiple "" or ; or even <<<.  I find it makes the string very easy to read and also most readily shows what is a variable and what is just the text for the string.  When I compare this the basic PRINT example I have to look carefully to realise name is the variable (well not too hard for a small string of course),  but this gets even harder with a large string say to update a database.   The f string would be something like the following:

uSql = f"UPDATE Current SET DateTime = '{myDate}', Value = '{myValue}' WHERE UnitID = '{myBoard}' AND Location = '{myLoc}' AND Sensor = '{mySensor}'"
cursor.execute(uSql)
 
In the above example the SQL syntax wants ' ' to surround the values hence they surround the {variable} names in the string.   But I'm in no doubt as the the fact the myDate is a variable and if I want to find out where and how myDate got its value I'm on to it right away.   Maybe you want to compare that to what a PRINT or cout would be for a large string? 
 
Hope I did not get too carried away here but I truly do love f strings.   Your milage differers and I'm not suggesting otherwise, just making my plug for f strings ? 

 


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2035
 
Posted by: @byron
... but I do point out that f is not a print command in Python, rather its a format statement.

Ok I didn't realise f had a print statement the examples just used the Python shell.
https://realpython.com/python-f-strings/
Yes f is the way to go if you are using Python.

You have to learn how each language does exactly the same thing with its own variations, additions and omissions when compared with each other. The Rosetta Code collection can be one way to compare how different languages tackle the same algorithm.

Of course anything is easy to read once you have practised it enough times. 

One other language I downloaded and have spent time learning is Processing  because there are lots of video tutorials on how to use it for things like images processing and video.
https://processing.org/
It can also be used with the RPi
https://pi.processing.org/get-started/
and Android operating system.
https://android.processing.org/
And of course you can communicate with the Arduino over the serial port.
https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all

 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robo-pi

Posted by: @robo-pi

YES!

This is exactly where I'm at right now.  My robot project is at a point where it's really starting to take off.  At least in terms of a lot of different facets finally "coming together".    In fact, the complexity is getting to a level when I've been thinking of just stopping and taking some time to regroup and draw up some flowcharts.

Indeed, your post of breaking up a program that was getting out of control actually prompted me to propose this topic.

Posted by: @robo-pi

I'm also writing part of the program in C# on my laptop and two other parts on the robot in C++

I think C# is a pretty good language, and I do like it a lot, though I've never really had many project's that require it's use.  I like WPF, as it's a lot like HTML5 grids and boxes and CSS too... a little difficult to go through the motions at first, but I like it's dynamic features... in fact, I was working on my own version of a G-CODE library a while back, and started building a GUI for a windows controller based on Windows Forms, then dropped it in favor of WPF, but never got back to it!  I'm always jumping from one project to another - I have too many things on the go 🙂

Posted by: @robo-pi

I wonder where we should post the thread?  We don't really have a sub-forum dedicated to flow charts, algorithms or overall programming structures.   All we have in the programming section is Arduino, Raspberry Pi, C++ and Python.

I could put it in the Artificial Intelligence section?

That sounds like a good idea!

Posted by: @robo-pi

Yeah, the Artificial Intelligence is where I'll post the fantasy skit. 

Cool, I will check it out! 🙂


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@casey,

Posted by: @casey

What were these tuples, dictionaries and lists about!! 

FYI: - The C++ Standard Library offers these too (since ~C++11 onward's for memory?), it's just that the minimal Arduino implementer's chose not to implement them, along with many other excellent container classes - I hope that one day they will be able to fit them in 🙂

 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@casey

Posted by: @casey

Yes f is the way to go if you are using Python.

The new C++20 standard due in June/July this year will also offer python style formatted strings, though may not be available for embedded systems.  Many new features are added every 3 years as, C++ is an ISO international standard. 


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @frogandtoad

I think C# is a pretty good language, and I do like it a lot, though I've never really had many project's that require it's use.

I don't even remember exactly how I got into C#.   At one time I was really big on Visual Basic.  Mainly because it made using Windows Forms very easy.   Then I got out of programming completely for quite a long time.  When I decided to return to programming again, I had trouble finding the old VB I was used to using.   So I ended up switching over to C#.    I don't even recall the details of exactly how that happened.    I think it also may have had something to do with wanting to use Microsoft Speech Platform.   I was interested in getting into Semantic A.I. (i.e. A.I. based on the meanings of words).     Apparently that's only one of the things that Semantic A.I. is used to refer to these days.  But that's a whole other story.

In any case it was the Microsoft Speech Platform that really got me hooked on C#.   One reason was that all the Microsoft examples were given in C#.  I imagine you could use the Speech Platform with other languages.  But all their documentation was done in C#.   The main thing that caught my interest was their Semantics Builder and Grammar Builder.  Those features appeared to be the perfect tools for writing Semantic A.I. programs.

It actually turned out that their method of dealing with semantics and grammar isn't really all that great.  So I ended up creating my own methods for dealing with semantics.  So now that I no longer need the Microsoft GrammarBuilder and SemanticKeys I've actually been thinking of mover over to writing my Semantic A.I. programs in Python.  Especially now that I've learned how to use Numpy, and several other Python tools.

I still like C# for Windows Forms applications though. Just for the Windows Forms GUI.   The only problem there is that they tend to only run on Windows machines.   I'd probably be better off moving over to some type of GUI Forms language that will run on either Windows or Linux. 

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robo-pi

Posted by: @robo-pi

At one time I was really big on Visual Basic.

Me too, but I finally severed that thing from my wrist's - The last version I had was 6.0 with the full MSDN library (at lease back then you got real documentation, along with video example... where do you get that these days?)

Posted by: @robo-pi

I was interested in getting into Semantic A.I. (i.e. A.I. based on the meanings of words). 

Hmmm, interesting!  I have no knowledge of AI whatsoever, but programming languages are actually based on the meaning of documented rules made up of words (tokens), whereby via parsing and lexical analysis, interpreters and compilers turn those words into on the fly programs or executable programs - Check out BNF or EBNF language notation if you want to create your own robot language one day, then write and interpreter and even compiler for it - Maybe the medulla part of the brain of an android should be a compiler? 😉

I'd really like to get back to C# one of these days... I may just get back to one of my unfinished projects!

Cheers!


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2035
 

@frogandtoad,

The question I have to ask myself is:  Can I write the programs I want to write with what I already know or do I need to learn these new additions to a language?

Its been over a decade since I last used C++ and when I decided to refresh my memory on how to program a game in C++ using the window's API from some books I had the code refused to compile with the code::blocks ide I had downloaded.  Apparently the code was written before c++ 11.  No doubt there was a work around but it made me decide not to bother with it.  All this learning needs ongoing practice and over the last decade or so I have been able to write the programs that interested me with the easy to use FreeBASIC.

Actually I just downloaded a c++ example of using tuples and ran it from code::blocks just to see what it looked like.  The reality is however if I want to get some things done in a reasonable time frame without too much effort I will have some high level language like Python or Processing and leave c++ to the professionals or those who have the time to put in the work required to use it.

 

 

 


   
ReplyQuote
Page 5 / 6