Attribute Error: ‘type’ object has no attribute ‘datetime’

While trying to write some test for NTP servers, my response time used this:
I am using IronPython 2.7.

timetoreturn = time.time()            
if(self.response_time != 0):                
timetoreturn = (self.response_time – datetime.datetime(1970,1,1)).total_seconds()

I got this error: Attribute Error: ‘type’ object has no attribute ‘datetime’.

My imports were:
import datetime
from datetime import datetime, tzinfo

Come to find out, using import datetime just causes problems so I removed that. That left me with ‘from datetime import datetime, tzinfo’.

Now. What the heck is the deal?

This is what. Datetime module has an object named datetime that you can use. Which is very confusing. So FROM DATETIME means that I am in the datetime module, importing datetime object to use, and from there I can just use the attributes. Instead, I was writing ‘datetime.datetime’ meaning I was already in the second level of inception (datetime > datetime) and then trying to access an attribute named ‘datetime’. So datetime.datetime.datetime.

This doesn’t work. 🙂

Just be aware of the level of the module you are in when importing and accessing attributes.

Udacity Kotlin Nanodegree: Course 1

I have been waiting for this course to come out for the past six months and now it is here! I was able to get a 50% off on the course so that was really helpful. So far, the course has been amazing and I have built my first app, a dice roller!

You can check it out here. https://github.com/esegebart/diceroller

This is what I have learned in my first course. I am posting this as reference to myself later.

Namespaces – tools – define dummy content that is only used when you preview the app in the preview pane

Attr using the namespace tools will be gone when the app compiles

Two namespaces at top: tool and android. Android is part of the core framework.

Gradle determines what devices can run your app, compiles app to executable, handles dependency management, automated testing, and app signing for Google play. Gradle packages your app into an apk with its resources, compiled code, Android manifest, etc and then transfers the apk to your emulator or device.

There are two Gradle files: build.gradle (project) and build.gradle (app). The app gradle file handles your different modules that has different functionalities, libraries, and supporting android wearables. In larger apps, each module will have its own gradle file. Inside the Gradle project file, there are repos that are available to the whole project: google() and jcenter().

Gradle also handles all the dependencies which is external code and libraries a project depends on. These are handled in the dependencies block.

dependencies {

           dependencies are managed here

           app dependencies do not go here

}

The app folder gradle file configures how to set up the app module and deals with the plugins to run and build Android and kotlin projects. This also tells the app the compilesdk, targetsdk, and minsdk. These are pretty straighforward. Min is the minimum version it can run on. The target will always match the compilesdk. This also has the appID which is a unique identifier and necessary for Google Play. It is the domain your app is hosted on, reversed, followed by the app name. So if my app was hosted on elyse.com and my app is named diceroller, it would be com.elyse.diceroller. This is set up when the project is started.

The version level you lists matches to an API level, and all api levels are named after tasty treats.

ANDROID JETPACK is the new android support libraries – androidx – and has all the support libraries for new and old apis.

APPCOMPATACTIVITY – The main activity extends from this. It is a compatibility class that makes sure activity includes a menu bar (action bar) that looks the same across OS levels and is a part of androidx. I never knew exactly what this was, so this was helpful.

Adding Vector Drawables: The proper way to use vector drawables and keep compatibility with older versions of android is followed.

  • In Gradle build.app file – under default config add vectorDrawables.useSupportLibrary = true.
  • Add app:srcCompat=”@drawable/image_name” in the ImageView tag
Add the namespace to the root of the layout xmlns:app="http://schemas.android.com/apk/res-auto"
  • This is using the support library to reference the image resource in the layout file and enable the support library for vector drawables in the app level build.gradle
  • THIS OPTIMIZES APP SIZE ON OLDER PLATFORMS
  • Open drawable (dice.xml) to see the colors and shapes listed in xml

build.gradle app file will generate png files that are use on those devices that are below minsdk. Png files will make app larger and make it slower. Large apps have high chance of being uninstalled. Androidx compat library supports devices all the way back to api level 7.

Adding app namespace supports either custom code or libraries in your project and not core android libraries.

Dependencies required to build the project or enable additional functionality are defined in the build.gradle.

Basic app structure:
Layouts – xml files the define what your app will look like
Activites – Kotlin classes where you write the dynamic parts of your app
Layouts and activities are connected by the process of layout inflation – and finding view objects by their unique id
TextView – displays text to the user
ImageView – display images to the user
Images are a drawable resource type.

Build gradle project in command line:
Change the directory of your project.
Type gradle run.
You can also include gradle run –warning-mode=all to see specific warnings.
Or you can type gradle tasks –info. Shows all tasks and what they are doing.

We Belong Here Podcast

I had an amazing experience this year to be on the We Belong Here Podcast with Lauren Lee Anderson (@lolocoding on Twitter).

I had heard a lot of podcasts about how they career transitioned from one career that was totally different and into tech. I didn’t hear a lot about the dark side of transitions. Transitions that come from dark, personal, life-changing experiences. My past is dark.

I got very vulnerable in this podcast to show others that it is your thoughts that allow you to keep going, a desire that keeps you going, knowing what you don’t want in your life that keeps you going.

Roadblocks happen, I believe they are supposed to. Success isn’t linear and those roadblocks build your resiliency. If you struggle with wondering if you can make it, you can. Have a listen.

I would love to hear your stories of how you overcame your obstacles!

Episode 21: Elyse Segebart
https://bit.ly/3bfXOyk

Elyse Segebart on the We Belong Here Podcast

Android N Developer Course: D5

Today I created a higher or lower game in Android Studio. Used Random class to generate a random number when the app launches.

Created a method that takes the user input when a button is clicked and converts the String to an int. Then I used If statement to take the int object and compare it to the random number that is generated. The app will tell the user higher or lower based on which statement is true. If the number is correct, it tells the user “That’s right! Try again!” and added the Random number code to generate a new number so the user can play again.

Went one step further and created a method called makeToast that takes a String object as its parameters and will display a message to the screen. So when the makeToast function is called, it will display the appropriate message.

This was super fun and it was great to see how to further simplify code by creating the makeToast function. I didn’t see it right away that it was possible, but with time I think I will get better at simplifying my code. And a photo of the app!

Android N Developer Course: D4

100 days of code: D8&9

Today I built currency converter application to take in Dollars and use toast to pop up the message in pounds. Even added the little squiggly pound sign to make it look more official. This was the end of the Android course section two. Next is the Java deep dive which I am really excited about. Some of the takeaways from the currency converter are using

String.format(“%.2f”, poundAmount)

In this example, the String.format sets the decimal point to two places and accepts the variable it will be formatting as the second parameter. If you are wondering what the “%.2f” means, like I did, %f is the format specifier for float data type in the functions printf and scanf. This format specifier will display up to six digits after the decimal point, but using %.1f or %.2f will make the precision to the first or second digit.

I added in the currency photo from Google and matched parent width.

I had to take the EditText (which is a String) and create a new variable and parse it to a Double. I added the conversion and assigned it to the variable poundAmount.

Then I used toast to display the conversion to pounds at the bottom of the screen.

Here is the code snippet:

And the application output!

Android N Developer Course: D3

100 days of code D6 & 7.

Created an application that takes two photos uploaded to the drawable folder. Added a button called New Cat and when clicked, will display a photo of a different cat. First tested that my button worked by using the Log.i to display a message to the log to ensure that my button was clicked. Created the onClick function called “clickCat”. Worked very similar to the EditText, only used ImageView and typecasted the findView to ImageView. The second part was to use

image.setImageResource(R.drawable.cat2);

to set the image to the second photo upon being clicked.

You can use Properties > Scale Type to change how the photo is displayed in the screen. FitXY stretches it to the edge of the container, fitStart keeps it in proportion and aligns to top of container, Center keeps it in proportion and centers the image. Center is probably one I will use the most.

And here are my results!

Beautiful cats aren’t they?