Thursday, October 25, 2018

macOS Mojave doesn't recognize git



Who doesn't want dark mode on mac!

However, after updating the macOs to Mojave, if you try to access git on your command prompt, it throws below error -

$ git
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun


All you need to do is reinstall the xcode command line tool using following command -

$xcode-select --install

It should run fine after that.

Thursday, June 21, 2018

GNU Octave - unable to run gnuplot

While going thru the coursera machine learning course exercise, I ran into below issue. It wouldn't allow to run some of the exercise code. 

gnuplot> set terminal aqua enhanced title "Figure 1" size 560 420  font "*,6" dashlength 1
                      ^
         line 0: unknown or ambiguous terminal type; type just 'set terminal' for a list

WARNING: Plotting with an 'unknown' terminal.
No output will be generated. Please select a terminal with 'set terminal'.

It's because we need to setup proper environment for it. Here is the quick solution -

>setenv("GNUTERM","qt")

Happy Learning!

Wednesday, July 26, 2017

Library not loaded: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib (LoadError)

I had grunt process defined to compile the sass files, it was working fine until I had to reinstall my brew and it end up breaking the sass compile compass. Below was the setting -

 compass: {
                options: {
                    cssDir: 'static/css',
                    raw: 'Sass::Script::Number.precision = 10\n::Encoding.default_external = \'utf-8\'\n',
                    sourcemap: true
                },
                styleSass: {
                    options: {
                        sassDir: '<%= appConfig.src.sass %>',
                        specify: ['<%= appConfig.src.sass %>/demo.sass', '<%= appConfig.src.sass %>/style.sass']
                    }
                },
....

On running the grunt, it failed with following error -

Running "compass:styleSass" (compass) task
Warning: Command failed: /bin/sh -c compass --version
/Users/J/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:126:in `require': dlopen(/Users/J/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin14.1.0/digest/sha1.bundle, 9): Library not loaded: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib (LoadError)
  Referenced from: /Users/J/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin14.1.0/digest/sha1.bundle
  Reason: image not found - /Users/J/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin14.1.0/digest/sha1.bundle
from /Users/J/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:126:i

if you run into an issue, do the following to fix it -

$ brew remove openssl
$ brew install openssl
or 

$ brew reinstall openssl

It should fix it!

Tuesday, July 25, 2017

Cannot read property 'localeCompare' of undefined

While running npm install, I ran into following error -

npm WARN engine webpack@2.7.0: wanted: {"node":">=4.3.0 =5.10"} (current: {"node":"5.0.0","npm":"3.3.6"})
npm ERR! Darwin 16.6.0
npm ERR! argv "/Users/J/.nvm/versions/node/v5.0.0/bin/node" "/Users/J/.nvm/versions/node/v5.0.0/bin/npm" "install"
npm ERR! node v5.0.0
npm ERR! npm v3.3.6

npm ERR! Cannot read property 'localeCompare' of undefined
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!
npm ERR! Please include the following file with any support request:
npm ERR! /Users/Jaimin/Apps/redesign/data-exploration/npm-debug.log

mostly you will run into this issue if you are running npm 3.3.5 or 3.3.6.

You can try resolve it by below two ways,

1. Remove all node_modules folder, and try to run 'npm install' again.

If above doesn't resolve it, try below -

2. upgrade npm to 3.3.7 (or any subsequent one)
npm install npm@3.3.7 -g

It should resolve the issue. If you want to read up various other suggestions/reasoning checkout here

Sunday, June 11, 2017

Instaling TensorFlow on mac OSX Yosemite


I wanted to try out TensorFlow, so I started with the installation steps. First create virtualenv, and then try to do pip install, there are bunch of different ways you can install as per the instructions provided here, though I went with below and it at least for the first part installed it right.

pip install --ignore-installed --upgrade \ https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py2-none-any.whl

Above is CPU-only installation with Anaconda. My mac default python runs under anaconda, so above worked well for me compared to other installation mechanism described in documentation.

After the installation I followed below step to try out test of tensorflow installation success -

$ python
>> import tensorflow as tf

Though I ran into error -
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description) 
ImportError: numpy.core.multiarray failed to import

It was because of the numpy, it was pointing to the wrong one. I tried below to check the path of numpy -

$ python
>> import numpy
>> print numpy.__path__
['/usr/local/lib/python2.7/site-packages/numpy']

Which is an issue, as its using /local/lib/python2.7 installation and not local virtualenv installation -
Go to the path and remove the file explicitly. After that I tried again to review the path of numpy -

>> print numpy.__path__
['/Users/jpatel/Apps/tensorflow/venv/lib/python2.7/site-packages/numpy']

However, at this point I run into another error -

    from google.protobuf import descriptor as _descriptor
ImportError: No module named protobuf

Its because of issue with protobuf installation version, after looking at few of the online documentation help,  I updated to below -

$ pip uninstall protobuf
$ pip install protobuf==3.0.0a3

though error continues, 

    from google.protobuf import any_pb2 as google_dot_protobuf_dot_any__pb2
ImportError: cannot import name any_pb2


so figured another version from online help -

$ pip install --upgrade protobuf==3.0.0b2


After that it started working fine. -

>>> import tensorflow as tf
>>> hello = tf.constant("Hello, TensorFlow")
>>> sess = tf.Session()
2017-06-11 09:45:35.310965: W t
>>> print(sess.run(hello))
Hello, TensorFlow

>>> node1 = tf.constant(3.0, tf.float32)
>>> node2 = tf.constant(4.0)
>>> print(node1, node2)
(, )


If I do more experimentation around it, will share more examples and issues I run into and how to solve..

Saturday, April 15, 2017

Generic relations in Django

While using django's content management system (admin), for adding different objects on it, you may want things like audit notes, that can be used for reference in the future or get some more info. you have two choice -


  1. Add a explicit field to the object, though the down side is you will need to add that extra field with all the objects where you want note.
  2. Create a generic model which can be used with different objects in your app. 

It's no brainer between above two choice, answer is 2nd. Generic relations are at help.

You can checkout django-contrib-comments, it's one of the example of generic relations. Let's take example of how Notes like foreign key reference can be added freely to any model in your app.

notes/models.py
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

class Note(models.Model):
    note = models.TextField("note",
                            max_length=2000)
    date = models.DateTimeField(auto_now_add=True)
    # Below the mandatory fields for generic relation
    content_type = models.ForeignKey(
        ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    class Meta:
        verbose_name = 'Staff Note'

    def __unicode__(self):
        return self.date.strftime("%B %d, %Y")


Now, this Notes object can be referenced with any other model you want to use it with. Here is one example -

mymodel/models.py
from django.contrib.contenttypes.fields import GenericRelation
from notes.models import Note


class MyModel(models.Model):
    name = models.CharField("Name", max_length=100)
    notes = GenericRelation(Note)

When you run migration, it doesn't add anything in MyModel, as its generic relation it gets its references via django content type and object id.

How to add it in admin?

notes/admin.py
from django.contrib.contenttypes.admin import GenericTabularInline
from .models import Note

class NoteInline(GenericTabularInline):
    model = Note
    extra = 0


mymodel/admin.py
from misc.admin import NoteInline
from .models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    inlines = [NoteInline, ]

admin.site.register(MyModel, MyModelAdmin)

And you are all set. It's easy to plug, efficient, and clean implementation. Good luck!

Django rest framework serializer with self-referential foreign key for comments

Django had default comments package sometime back (django.contrib.comments), since it's deprecated, there is external repo available for now.  It provides flat comments, so if you want threaded comments, you can use django-threadedcomments.

We are using django-rest-framework, and access the threaded comments via drf.

resources.py

class ObjectSerializer(serializers.ModelSerializer):

    comments = CommentsSerilizer(
        source='comments_set',
        many=True)

    class Meta:
        model = ObjectName
        fields = (
            "id",
            .....,
            "comments",
        )


serializers.py

from rest_framework import serializers
from .models import FluentComment

class RecursiveField(serializers.Serializer):
    def to_representation(self, value):
        serializer = self.parent.parent.__class__(
            value,
            context=self.context)
        return serializer.data

class CommentsSerilizer(serializers.ModelSerializer):
    children = RecursiveField(many=True)

    class Meta:
        model = FluentComment
        fields = (
            'comment',
            'url',
            'submit_date',
            'id',
            'children',
        )

Recursive Field is one way to get the self-referential objects via serializer, it handles parent-child relationship. Here is another way to do it -


class CommentsSerilizer(serializers.ModelSerializer):
    user = UserLightSerializer()

    class Meta:
        model = FluentComment
        fields = (
            'user',
            'comment',
            'submit_date',
            'id',
            'children',
        )

CommentsSerilizer._declared_fields[
     'children'] = CommentsSerilizer(many=True)


Above both options are fine, and it should do the magic. Good luck!