Tech
Infected

Linux, OpenSource, Programming And Hacks

May 19, 2019

Dynamically Create JSON with Foreign Fields nested in Django

May 19, 2019
I struggled a lot to create a JSON object which should also contain foreign field values.
So after Googling and trying a lot, I came up with this masterpiece code :)
This code is very simple does not require any JSON serialization.

In the code below, we are creating a JSON objects for a Quiz app with Question Model, and Choices Model
the choices model is a foreign field (one to many - one question can have many choices) to Question model.

we first convert the queryset to a list so it's easier to iterate.
rest you can understand by looking at the code itself.

allQuestions = list(Question.objects.all())

data = []

for questionObj in allQuestions:
 item = {"id":questionObj.id, "question": questionObj.question, "choices":[]}

 for choiceObj in questionObj.choices.all():
  item["choices"].append({"position":choiceObj.position, "choice":choiceObj.choice})

 data.append(item)



Data will contain JSON like shown here

[
 {
  "id": 2,
  "question": "What is the capital of India?",
  "choices": [
   {
    "position": 1,
    "choice": "Mumbai"
   },
   {
    "position": 2,
    "choice": "Delhi"
   },
   {
    "position": 3,
    "choice": "Goa"
   }
  ]
 },
 {
  "id": 3,
  "question": "Why so serious?",
  "choices": [
   {
    "position": 1,
    "choice": "Batman"
   },
   {
    "position": 2,
    "choice": "Joker"
   },
   {
    "position": 3,
    "choice": "Monkey"
   },
   {
    "position": 4,
    "choice": "Bunny"
   }
  ]
 }
]

May 12, 2019

How to Shuffle (Randomize) QuerySet in Django

May 12, 2019
Suppose you want get a random item from a QuerySet, It should be random but non repeating.
In many posts I saw they use my_list = MyModel.objects.order_by("?") but it will produce repeating items.


1st Step: get a QuerySet

my_qset = MyModel.objects.all()

2nd Step: convert it into a list

my_list = list(my_qset)

3rd Step: Shuffle the list

shuffle(my_list) # from random import shuffle

4th Step: Use a for in loop to iterate and check each item.

for item in my_list:
    print(item.id)


So simple! and that's it.

May 2, 2019

An online Drum Machine I built using Web Audio API

May 2, 2019
Hey guys today I want to share a Drum Machine application that I built using. https://www.onlinetool.in/drum-machine/. Used Web Audio API to built it. The web audio api was initially proposed by google is used for processing and synthesising audio in web applications.

Initially, I tried building this application using the HTML5 audio element but it caused inconsistent timing and low latency.

A drum machine (a type of music sequencer) is basically used to program your own rhythm. It imitates a real drum kit. It can be used as a backing track to play any instrument like guitar, synthesizer... Its helpful in writing your own songs and accompanying in live performances.


Users can set their custom beat pattern or select any of the given preset patterns like rock, funk, hiphop. The pattern can be changed in real time (on the fly) without any interruption. It has a 16th note timing grid. Tempo can be adjusted using the input field.

Interface is designed using bootstrap and logic using VueJs and JavaScript/JQuery.


So far its a basic application. I have plans to further extend its functionality like:

  • individual gain control over the pads like Kick, Hithat, Shaker
  • adding effects like reverb, echo, panning and different filters
  • graphic visualiser
  • Users will be able to save their patterns as a .wav file and play it anywhere.


I will come up with another post describing its technical flow.

Hope you like it and thanks for reading :)




Apr 3, 2019

Apache Tomcat option not showing in servers in Eclipse

Apr 3, 2019
If you cant find Apache tomcat server option in the Server adding option follow the steps below.

1. Search Apache Server in market place

and install Eclipse JST server Adapters like shown in below image





2. After restarting eclipse you can find now find different Tomcat versions to select.


To install Tomcat follow this article.

Mar 31, 2019

How to Install latest version of Eclipse IDE in Ubuntu 18.04

Mar 31, 2019
In this article we will see how to install the latest version of Eclipse IDE in Ubuntu 18.04.

We will use Snappy Package system since the the version in the Ubuntu repository is outdated and also has some problems after installation.

Eclipse is built on top of Java so JRE is required. Just install OpenJDK which contains the required JRE. use the following command and you are good to go.

sudo apt-get install openjdk-8-jdk

Give the following command to install Eclipse with Snappy tool.

sudo snap install --classic eclipse

Upon successful installation you can see the below message in the terminal

eclipse 4.8.0 from 'snapcrafters' installed

Congrats! now the latest version of eclipse is installed on your Ubuntu machine.

You can launch eclipse by searching in the Dash (Press Window Key)

Jun 17, 2018

How to Create WIFI Hotspot from Ubuntu 18.04 Laptop

Jun 17, 2018
To create a wifi hotspot in ubuntu 18.04, you should be first connected via LAN.

The process is quite simple and can be done via GUI.

Step 1: 

Click on the top right buttons and click on the WiFi option. in the dropdown select Wifi-settings.

May 31, 2018

Python Program to Add two binary numbers using built function

May 31, 2018
Here we will see how to easily add two numbers in python3 using built-in functions.

please run this program with python3.

Steps:

1. Take input of the two binary numbers as string using the input function.
2. convert them into integers using int function passing the base 2
3. Add the integers
4. convert the sum into binary using the bin function

Same steps for subtracting, multiplication, or division, just change the operation in step 3

a = input("Enter 1st binary number: ")
b = input("Enter 2nd binary number: ")

#converting a and b into integer

a = int(a,2)
b = int(b,2)

#adding the integers

s = a + b

#sonverting the sum into binary

s = bin(s)

print(s)

You can take this Python Quiz



May 29, 2018

How to show IP address in Ubuntu 18.04 (Connection Information)

May 29, 2018
As we know that Ubuntu 18.04 ships with Gnome Desktop by default. There used to be a Connection Information option in previous Unity Ubuntu versions which showed IP address, Netmask,  DNS address, and various other infos but it's no longer shown.

But you can view the connection information on Ubuntu 18.04 as follows.

May 26, 2018

Shell Script to Run Django Development Server Automatically

May 26, 2018
We all know how boring it can be to start the django development server each time. entering the same commands over and over. The good news is that you can write a simple shell script to do so. and after that you just need to run the shell script and yay! your django server is running automatically with just a little command. Cool!!

Let's say you have a Django project named project and a virtual environment named env inside a directory called project_folder

cd project_folder
source env/bin/activate
cd project
python manage.py runserver

save this script as script.sh or whatever you want.

Run this script using . (dot) rather than ./script.sh

We do this because if we run the script normally with a ./ then the cd command in the script file will get back to the current directory after changing it.

. script.sh



Tip: you probably wanna wanna place this script at your home directory and give a small name like dj.sh or anything so that it's easier to type in.

But it's better to make an alias of executing this script and place it at the end in your .bashrc file

nano .bashrc
alias dj='. $HOME/script.sh'

and now restart the terminal.

From now on, all you need to do is just type dj in the terminal and it will do the job for you!


May 22, 2018

How to upgrade Ubuntu 16.04 to 18.04

May 22, 2018
Ubuntu 18.04 aka Bionic Beaver was released on 26 April this year. This is a LTS (Long Term Support) Release. It was one of the most awaited release version.

In this tutorial we will see how to upgrade ubuntu 16.04 (Xenial Xerus) to 18.04

Ubuntu allows users to upgrade one LTS version to another. since ubuntu 16.04 is also an LTS version so it can be upgraded directly to 18.04. for non LTS version it is not possible.

May 18, 2018

How to Install Gummi LaTex editor for Ubuntu 18.04 / 16.04

May 18, 2018
Gummi is a Latex Editor. it is available for both Linux and Windows platform. Gummi has a Live preview, unlike command line tools where you need to compile the tex file to product the pdf. It supports insertion of tables and images via gui. Bibliography management is supported.


It is available for Linux Distributions like Debain, Ubuntu, Linux Mint, Fedora, Gentoo, Arch Linux..

Install in Ubuntu 18.04


sudo apt install gummi



Install in Ubuntu 16.04 / 16.10 / 17.04 / 17.10


sudo add-apt-repository ppa:gummi/gummi
sudo apt-get update
sudo apt-get install gummi

Apr 2, 2018

How to install Google Chrome on Ubuntu / Linux mint [via PPA]

Apr 2, 2018
Google Chrome, the most popular browser, is available for Linux. While Chromium, the open source version of chrome is there by default in ubuntu repository, we need to add google chrome source to the repository by ourself.



Follow the below steps to install Google Chrome in Linux mint / Ubuntu 14.04 , 16.04

Apr 1, 2018

How to compile and run Java program in Linux / Ubuntu Terminal

Apr 1, 2018
Writing a Java program is quite straight forward in Linux systems, no matter what linux distro (Linux Mint / Ubuntu / Debian / Arch Linux) you have, the steps will same except for step 1 which is installation, it may differ distro to distro

Here we will write, compile and execute a simple HelloWorld Program.

Mar 24, 2018

Ipython, an alternative Interactive Python interpreter [tutorial]

Mar 24, 2018
Ipython is an interactive python shell which can be used as a replacement for your default python interpreter.

Install Ipython


pip install Ipython

Launch Ipython


Just enter ipython in your terminal

aman@vostro:~$ ipython
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

Mar 23, 2018

Rearrange Bootstrap Column Order with Push and Pull Classes

Mar 23, 2018
Suppose you have three columns, left-sidebar, main-content, right-sidebar. when the page is opened on small screen size the left-sidebar will be stacked at the top followed by the main-content and right-sidebar.

What if you wanted to get the main-content at the top followed by the left-sidebar and right-sidebar?

This can be accomplished by the bootstrap column push and pull classes.

<div class="row">

 <div class="col-sm-8 col-sm-push-2">
  <div style="background-color:red;color:#fff">main-content</div>
 </div>

 <div class="col-sm-2 col-sm-pull-8">
  <div style="background-color:blue;color:#fff">left-sidebar</div>
 </div>

 <div class="col-sm-2">
  <div style="background-color:blue;color:#fff">right-sidebar</div>
 </div>

</div>

on large display


on mobile display