Category Archives: tips

How to install pip and create virtualenv on Windows without administrative permission

Since Python 2.7.9, pip is released together with the Python. But I still cannot find it when our administrator installed the latest python on my PC. How to install pip, then create virtualenv on Windows without the administrative permission?

  1. Download get-pip.py.
  2. Run python get-pip.py --user. It will install pip locally.
  3. Install virtualenv by running python -m pip install --user virtualenv. This will install the package of virtualenv
  4. Run python -m virtualenv ENV to create a new virtual environment. ENV is a directory to place the new virtual environment.
  5. Activate the script by running ENV/Scripts/activate.bat.

read more

Where to place you own .sty or .cls files, to make them available to all my .tex files?

Suppose you have defined a package or a class, but how do you make it available at all times? One way is to just place it in the same folder as the .tex file, but that’s not what I’m after here. This post will tell you a way to “install” the package centrally on your computer.

1. Find out the directory. First, use this command at the command prompt to find out where your TeX home directory is.

kpsewhich -var-value=TEXMFHOME
OS Location
Windows C:/Users/stefan/texmf
Unix-type ~/texmf/
Mac ~/Library/texmf

2. Place files in the “right place”. Following the TeX directory structure, you should place your file in the “right place” like ~/texmf/tex/latex/packagename/packagename.sty. For a TDS-conformant system (TeX Directory Structure), the “right place” for a LaTeX .sty file is a suitably-named subdirectory of texmf/tex/latex/. For a full list of “right place”s, please see LaTeX/Installing Extra Packages. Moreover, for the beamer style, put the theme under beamer/themes/ with “color”, “font”, “inner”, “outer”, and “theme” in seperated subdirectories.

read more

Dropbox icon is not working Xubuntu 16.04 LTS

After upgrading to Xubuntu 16.04, I found that the dropbox icon stopped working. The icon is black with a red slashed zero. I cannot click on it to bring up the dropbox menu.

The reason is that Dropbox starts to use “indicator area” for their tray icon instead of “notification area”. The problem can be fixed by running

$ dropbox stop && DBUS_SESSION_BUS_ADDRESS="" dropbox start

or

$ dropbox stop && dbus-launch dropbox start

How to submit a package to PyPI

How to submit a package to PyPI

PyPI (Python Package Index) is a repository of software for the Python programming language. This article will tell you how to submit your package to PyPI, so others are able to install it with easy_install or pip. The offical document is at http://wiki.python.org/moin/CheeseShopTutorial#Submitting_Packages_to_the_Package_Index

Create your accounts

You must create accounts on PyPI Live and PyPI Test before upload your code.

After registration, create a pypirc configuration file at ~/. This file holds your information for authenticating with PyPI, both the live and the test versions.

[distutils]
index-servers =
  pypi
  pypitest

[pypi]
repository: https://pypi.python.org/pypi
username: {{your_username}}
password: {{your_password}}

[pypitest]
repository: https://testpypi.python.org/pypi
username: {{your_username}}
password: {{your_password}}

Prepare your package

Every package on PyPI needs to have a file called setup.py at the root of the directory. If you are using a markdown-formatted README file you’ll also need a setup.cfg file. Also, you’ll want a LICENSE.txt file describing what can be done with your code. So the directory structure would look like this:

root-dir/
  setup.py
  setup.cfg
  LICENSE.txt
  README.md
  mypackage/
    __init__.py
    file1.py
    file2.py
    file3.py

setup.py

This is metadata about your library.

from distutils.core import setup
setup(
  name = 'mypackage',
  packages = ['mypackage'],
  version = '0.1',
  description = 'A random test lib',
  author = 'Peter Downs',
  author_email = 'your email address',
  url = 'https://github.com/yfpeng/mypackage',
  keywords = ['testing', 'logging', 'example'],
  classifiers = [],
)

read more

How to create a Web Application Project with Java/Maven/Jetty

How to create a Web Application Project with Java/Maven/Jetty or Tomcat

In this article, we create a simple web application with the Maven Archetype plugin. We’ll run this web application in a Servlet container named Jetty, add some dependencies, write simple Servlets, and generate a WAR file. At the end of this article, you will also be able to deploy the service in Tomcat.

System requirements

Creating the Web Service Step by Step

This section explains how to create this simple web project from an EMPTY folder.

Creating the Simple Web Project

To create your web application

$ mvn archetype:generate -DgroupId=com.pengyifan.simpleweb \
      -DartifactId=simple-webapp \
      -Dpackage=com.pengyifan.simpleweb \
      -DarchetypeArtifactId=maven-archetype-webapp \
      -Dversion=1.0-SNAPSHOT \
      -DinteractiveMode=false

...
[INFO] BUILD SUCCESS

Once the Maven Archetype plugin creates the project, change the directory into the simple-webapp directory and take a look at the pom.xml. You should see the

<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.pengyifan.simpleweb</groupId>
  <artifactId>simple-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>simple-webapp</finalName>
  </build>
</project>

read more

How to collect Immutable Collection in Java

To begin with this story, let’s first have a look at how to creat a List from Stream in Java

List<String> sublist = list
  .stream()
  .filter(...)
  .collect(Collectors.toList());

This works perfectly fine but what if we want the list to be immutable? We could do this

List<String> immutableSubList = Collections.unmodifiableList(sublist);

or if we would like to use Guava ImmutableList, we could do

ImmutableList<String> immutableSubList = ImmutableList.copyOf(sublist);

However this is a bit awkward to use since the list will be copied one more time. If we want to do this in a lot of places throughout the code base, it is not fluid. Instead, what we want is

ImmutableList<String> sublist = list
  .stream()
  .filter(...)
  .collect(ImmutableCollectors.toList());

This post will discuss how to create the Collector of ImmutableList.

Collector

To create a Collector, we will use the static method of.

public static<t, A, R> Collector<T, A, R> of(
  Supplier<A> supplier,
  BiConsumer<A, T> accumulator,
  BinaryOperator<A> combiner,
  Function<A, R> finisher,
  Characteristics... characteristics);

read more

How to convert sourcecode to HTML, RTF, SVG, etc.

I am working on my paper which needs a piece of XML to syntax highlighted. I’ve found Sublime with “Copy as RTF” plugins is useful, but as a programmer I prefer something that being done via commend line, and more importantly being easily customizable.

So I did a some searches and came across highlight. To install it on Ubuntu is quite simple

sudo apt-get install highlight

Then I can use highlight to convert the XML file to RTF and copy it to the paper I am working on.

read more

Resolve coreference using Stanford CoreNLP

Coreference resolution is the task of finding all expressions that refer to the same entity in a text. Stanford CoreNLP coreference resolution system is the state-of-the-art system to resolve coreference in the text. To use the system, we usually create a pipeline, which requires tokenization, sentence splitting, part-of-speech tagging, lemmarization, named entity recoginition, and parsing. However sometimes, we use others tools for preprocessing, particulaly when we are working on a specific domain. In these cases, we need a stand-alone coreference resolution system. This post demenstrates how to create such a system using Stanford CoreNLP.

Load properties

In general, we can just create an empty Properties, because the Stanford CoreNLP tool can automatically load the default one in the model jar file, which is under edu.stanford.nlp.pipeline.

In other cases, we would like to use specific properties. The following code shows one example of loading the property file from the working directory.

private static final String PROPS_SUFFIX = ".properties";

  private Properties loadProperties(String name) {
    return loadProperties(name, 
       Thread.currentThread().getContextClassLoader());
  }
  
  private Properties loadProperties(String name, ClassLoader loader) {
    if (name.endsWith(PROPS_SUFFIX))
      name = name.substring(0, name.length() - PROPS_SUFFIX.length());
    name = name.replace('.', '/');
    name += PROPS_SUFFIX;
    Properties result = null;

    // Returns null on lookup failures
    System.err.println("Searching for resource: " + name);
    InputStream in = loader.getResourceAsStream(name);
    try {
      if (in != null) {
        InputStreamReader reader = new InputStreamReader(in, "utf-8");
        result = new Properties();
        result.load(reader); // Can throw IOException
      }
    } catch (IOException e) {
      result = null;
    } finally {
      IOUtils.closeIgnoringExceptions(in);
    }

    return result;
  }

read more

How to access file resources in JUnit tests

In maven, any file under src/test/resources is copied to target/test-classes. How to access these resource files in JUnit? Using the class’s resource. It will locate the file in the test’s classpath /target/test-classes.

URL url = this.getClass().getResource("/" + TEST_FILENAME);
File file = new File(url.getFile());