OOP : Name Error while trying to make a object instance - python-3.x

I have a class called FIT, saved in a file called manage.
in my main file, the first lines look like this
import manage
FITobj= FIT()
I thought when I did this it would call the class so I would be able to use functions like get_balance() like FITobj.get_balance(). but instead, when I try to run my program I get name error name FIT is not defined. I'm fairly new to object-oriented programming, can someone help?

You need to indicate where the FIT class is defined. Either change it to:
import manage
FITobj = manage.FIT()
Or
from manage import FIT
FITobj = FIT()
The first is slightly more typing, but it helps keep your code more readable because as you import more and more, it can get very difficult to keep track of where everything is coming from.

Adding to #mypetlion's answer, to avoid even more typing (but make your program even less readable), you can do this:
from manage import *
FITobj=FIT()
And then use anything else from manage as well as just FIT.

Related

Processing insists pause() is not a function, when it very much is

Aite, [first poster here, pls don't bash]
So, I'm using the sound library, which I of course remembered to import, and works just fine, proof being given by the fact that all the other functions I used work as expected and give no problems neither in editor nor in execution.
Except, of course, for this little bugger of a pause() function, which I wrote as per below using no different a syntax from all the other functions, only to find out Processing isn't very keen on accepting its existence.
Problem shows both using 3.3.6 and 3.5.
Oh, and also, apparently isPlaying() returns an int, what's up with that?
If, as I'd suspect, that single line below won't be enough code to couple with the info to get to the bottom of it, here's a Dropbox link to the code (since it uses a bunch of files) so you can test it yourself.
It kinda won't work if you try to run it as is tho because it messes up when trying to load all the songs (in the last line of setup), yeah I kinda need some help with that too... works fine if you only load the first one tho!
https://www.dropbox.com/sh/di7mwit0w2l4513/AABipGDAdoKx277f8Hg_ZfhDa?dl=0
(Please, don't expect clear, extensively commented coding. I started working on this way before I learnt that was a thing. Deeply sorry. Of course, you can ask away about anything baffling you)
What did I try, er, writing it well???
I used .play(), .stop(), the volume ones, and they all, as per stated, work fine.
import processing.sound.*;
SoundFile[] songs= new SoundFile[1];
void setup(){
songs[0]=new SoundFile(this,"Small Bump.mp3");
songs[0].play();
}
void draw(){
}
void keyPressed(){
if (songs[0].isPlaying()==1)songs[0].pause();
}
When I copy your code into my Processing editor, I get a couple errors:
songs[0]="Small Bump.mp3";
The sounds array holds instances of SoundFile, but you're trying to store a String value here. Maybe you're looking for the SoundFile constructor?
if (songs[0].isPlaying()==1)
The isPlaying() function returns a boolean value, but you're comparing it to an int value.
songs[i].pause();
You haven't declared this i variable anywhere. Probably meant for this to be a 0.
If I fix all of these errors, then your code compiles fine.
You might want to take a look at the reference for the Sound library here.
The Sound library I had installed was 1.3.2, or something of the likes.
All the references I'd read were for 2.0+.
Having updated that through the "add library" menu, all was solved.

Preload/Globally certain classes in Python

I Have got some models (mapped on database tables), in a python project. I want to make them preload when the project bootup so i don't need to do like this in every file
from db.models.user import User
I tried to load them in init.py file, but they are not available.
TL;DR: You need to import your models in every file.
In Python, every module (*.py file) has its own namespace (set of global variables). There is no way to have a truly global variable (or class, function, etc.), because every name is local to the module in which it is defined. This is a deliberate feature of Python; it would be very bad if (for example) you could redefine the range() function in some random module and break everyone else's code.
You can place the import from db.models.user import User in db/models/__init__.py, but that won't make User globally accessible. Instead, it will create an alias db.models.User for the original class (db.models.user.User), which you still have to import as normal. This may save you a small amount of typing when importing the model in other places, and is a great way to flatten deep hierarchies of packages, but it does not make anything truly global, because you cannot make things global.

What is supposed to be in __init__.py

I am trying to create a Python package. You can have a look at my awful attempt here.
I have a module called imguralbum.py. It lives in a directory called ImgurAlbumDownloader which I understand is the name of the package -- in terms of what you type in an import statement, e.g.
import ImgurAlbumDownloader
My module contains two classes ImgurAlbumDownloader and ImgurAlbumException. I need to be able to use both of these classes in another module (script). However, I cannot for the life of me work out what I am supposed to put in my __init__.py file to make this so. I realize that this duplicates a lot of previously answered questions, but the advice seems very conflicting.
I still have to figure out why (I have some ideas), but this is now working:
from ImgurAlbumDownloader.imguralbum import ImgurAlbumDownloader, ImgurAlbumException
The trick was adding the package name to the module name.
It sure sounds to me like you don't actually want a package. It's OK to just use a single module, if your code just does one main thing and all its parts are closely related. Packages are useful when you have distinct parts of your code that might not all be needed at the same time, or when you have so much code that a single module would be very large and hard to find things in.

Why should I import "java.util.*" in top of my code?

my question is that i want to use Scanner object or Arrays object for example for usage of Arrays.copyof, but before importing java.util.* or java.util.Scanner and java.util.Arrays there is now object of them to use !
why this happens to me??enter image description here
It is a little unclear what you want to achieve. Are you asking why you should do imports in Java?
import statements allow you to refer to classes which are declared in other packages without referring to their full package name. This is standard java practice. Inside your main(String args[] you can use java.util.Scanner myobject = new .. if you want, but that is too cumbersome and often you have multiple classes in a package that you want to use. So adding a simple import java.util.* is considered better.
Another tip is to use one among the large number of available IDEs (eclipse, sublime, IntelliJ) which will add the imports automatically for you.

Creating libraries that can be imported and used in Groovy

Currently, I am working on a project to transpile from my company's in house scripting language, which is Object Orientated and takes quite a few features from other languages, into Groovy, which has many similar features.
To keep code as close to original as possible, I am trying to leave certain function names and parameters the same. To cater for this, I would like to write a set of libraries that can be imported.
For example, say I have an inbuilt method in the original scripting language,
I would like to be able to write the definition for this method in a groovy file, that can then be imported when needed, and the method may be called.
Tools.groovy
// filename: Tools.groovy
public String foo(String bar) {
return bar;
}
and in another file
Main.groovy
// filename: Main.groovy
import Tools;
String bat = foo("bar")
I know you can can compile class files into jars and put them into the class path, but a lot of the methods I will need to implement will either require meta programming or won't be associated with an object.
Sorry if it's either a bad question or not clear enough. I'm not sure whether its even possible.
Cheers
I believe you should be able to create libraries and reuse them when needed.
All you need to do is create class and add the static methods if you do not have to create instances, non static methods otherwise. Then it looks like you already aware how to proceed later.
For instance, you can create utilities classes for String, List, etc based on your description.
By the way, even if you do not create libraries, it is even possible to write one lines in groovy achieve what you may needed most of the cases.

Resources